Apache invisible URL Rewrite to subdirectory - apache

I want to use URL Rewrite to rewrite all requests on my domain to a subdirectory. But the visitor hould not see, that it is a subdirectory. I tried a view things, but nothing worked really good. Can you help me? In this subdirectory is also a .htaccess file and this file shouldn't be ignored by the server.

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule (.*) sub-directory/$1 [L]

Related

htaccess rewrite to https is adding unwanted folder

I don't normally use Apache and I am struggle to get a rewrite rule working.
The root folder contains a folder called public_html, when you browse to the website it loads as expected, I assume Apache automatically finds the index file inside the public_html folder. The problem I have is when adding the rewrite rule from http to https it adds the public_html to the url and a Not Found error is produced.
This is the rewrite rule I was using:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://exampledomain.com/$1 [R=301,L]
The url produced is something like this:
https://www.exampledomain.com/public_html
The desired url would look like this:
https://www.exampledomain.com
There are two parts to this questions:
Should the htaccess file be in the root or the public_html folder?
How do I prevent the rewrite rule adding public_html?
Any help would be greatly appreciated and if you would like further information or feel something is unclear please let me know.
Since this is a shared hosting, there are probably some rewrites already configured in server configuration (like adding /public_html prefix to every request).
You can try to workaround with:
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^public_html/(.*)$ https://exampledomain.com/$1 [R=301,L]

htaccess rule without noticable redirect

i have the following redirect rule
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^$ /folder/
basically my index.html file is in folder.html and instead of copy and pasting the index.html file from /folder/ i was trying to use the rule above to request the file and output the file as mysite.com but instead it is redirecting to mysite.com/folder/index.html. I assume this is possible to do as frameworks use the rewrite rule and this doesn't redirect the browser. Can anyone advise on what i am doing wrong?
Thanks in advance.

htaccess mod_rewrite URL within subfolder

My question seems to be unanswered on StackOverflow, so here goes:
I want to rewrite the following URL using an htaccess file which is in the root folder.
The URL to rewrite is this:
http://www.domain.com/subfolder/item/12345
to this:
http://www.domain.com/subfolder/item.php?id=12345
However nothing I seem to do works. I can successfully rewrite the item.php URL if it is in the root folder using this:
RewriteRule ^item/(.*)$ item.php?id=$1 [NC,L]
... but not if the item.php file is in a subfolder!
You can use following .htaccess in your subfolder:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subfolder
RewriteRule ^item/(.*)$ item.php?id=$1 [NC,L,QSA]
Your rewritten URL would become: http://domain.com/subfolder/item/123
Here is an example for sending this URL (The one entered in the browser address bar):
http://www.domain.com/subfolder/item/12345 (Can be any number) to this rewrited URL:
http://www.domain.com/subfolder/item.php?id=12345
RewriteEngine on
RewriteRule ^subfolder/item/([0-9]+)/?$ subfolder/item.php?id=$1 [L]
Those directives will work only if there is a subfolder directory and in that directory there is in fact a file item.php

Apache - rewrite images to php file with .htaccess

I'm looking for a way to rewrite all my image requests from one folder into some.php file, while preserving the original image url (or partial path).
So,
example.com/folder/img/test.jpg
would be rewrited as something like
example.com/folder/some.php?img=img/test.jpg
(is this the best approach?)
I'm not familiarized enought witrh regular expressions, so I'll be very thankfull :)
note : I've tried some solutions before, none of them worked. ALso, I'm running Apache 2.0 under CentOS environment.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]
Make sure:
.htaccess is enabled
mod_rewrite is enabled
Your URL is http://example.com/folder/img/test.jpg
It sounds like you you want the filename of the image in the url to be included in the new php url, not the entire url. So something like:
RewriteRule ^folder/img/(.*[.]jpg)$ /folder/some.php?filename=$1
Considering what you mention in the comments and that the previous rules didn't work, I edited the message, this is what i have now.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^folder/img/([\w]*\.jpg)$ folder/some.php?img=img/$1[R=301,L]
If folder is al variable, you can change that for (\w*) and add the reference in the right side of the rule.
Hope this helps.
Bye

.htaccess url rewriting /project/Login to /project/public/index.php/Login without redirecting URL

I have this .htaccess code that works perfectly:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ public/index.php [QSA,L]
</IfModule>
...but this code redirects to public sub directory. I don't know if it's possible to rewrite url without redirecting, just use /project/Login appointing to /project/public/index.php/Login.
RewriteRule project/Login$ project/public/Login [L]
Have you tired something like that? Your second link doesn't seem right "/project/public/index.php/Login", you shouldn't specify 'index.php' followed by another folder unless you have a RewriteRule that can handle it, otherwise the page doesn't exist on your server.
'/project/public/index.php?Login' (same as '/project/public/?Login') would be valid though, having the query string accessible as $_GET['Login'].
Hope this helps.