Redirect to internal web-directory - regex

I already found this script to "change the documentroot":
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.your-domain-name.com$
RewriteCond %{REQUEST_URI} !new-folder/
RewriteRule (.*) /new-folder/$1 [L]
It's simple and cool, but I want to complement the script. I want to redirect to the file /new-folder/index.php if the requested file does not exist inside the directory /new-folder/, so not only %{REQUEST_URI} !-f!
But I don't know how to get the current script-path, like __DIR__ (PHP) in .htaccess.
So my question is, how to check if a file exists inside a subdirectory (new-folder)?
My redirect will theoretically look like:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.your-domain-name.com$
RewriteCond %{REQUEST_URI} !./new-folder/
RewriteCond (PATH+Filename, if no filename is set, take index.php) !-f
RewriteRule (.*) ./new-folder/index.php [L]
The above code uses ./ to check relative, it does not know if it's inside the root!
Also I would like to manipulate the REQUEST_URI: replace the /new-folder/ from it, so scripts inside this folder don't have to do it

You can use it like this in your root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?your-domain-name\.com$ [NC]
RewriteCond %{REQUEST_URI} !/new-folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/new-folder/$1 !-f [NC]
RewriteCond %{DOCUMENT_ROOT}/new-folder/$1 !-d [NC]
RewriteRule (.*) /new-folder/index.php [L]
Another option is use this in any sub-directory /anydirectory/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}new-folder/$1 !-f [NC]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}new-folder/$1 !-d [NC]
RewriteRule (.*) %{ENV:BASE}new-folder/index.php [L]

Related

.htaccess rewrite rules for multiple sites

Scenario
I have 3 domain names:
www.hellokitty.com
www.keroro.com
www.doraemon.com
I want to host all of them in one web hosting with the following directories:
./htdocs/hellokitty.com/
./htdocs/keroro.com/
./htdocs/doraemon.com/
All of them are linked to the same hosting server with A record.
However, my hosting do not support virtual host.
I need .htaccess files to rewrite the URLs
So, I tried in this way:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?hellokitty.com$ [NC]
RewriteCond %{REQUEST_URI} !^/hellokitty\.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /hellokitty\.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?hellokitty.com$ [NC]
RewriteRule ^(/)?$ hellokitty.com/index.php [L]
RewriteCond %{HTTP_HOST} ^(www.)?keroro.com$ [NC]
RewriteCond %{REQUEST_URI} !^/keroro\.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /keroro\.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?keroro.com$ [NC]
RewriteRule ^(/)?$ keroro.com/index.php [L]
RewriteCond %{HTTP_HOST} ^(www.)?doraemon.com$ [NC]
RewriteCond %{REQUEST_URI} !^/doraemon\.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /doraemon\.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?doraemon.com$ [NC]
RewriteRule ^(/)?$ doraemon.com/index.php [L]
Thus, if I go to www.hellokitty.com, it rewrites to ./htdocs/hellokitty.com/ internally.
Problem
If I go to www.hellokitty.com/hellokitty.com/
Or, if I go to www.hellokitty.com/keroro.com/, it still works
So, my problem is that how to prevent the client direct access the above real URLs and show a 404 error to them?
Insert this rule just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /(hellokitty|keroro)\.com [NC]
RewriteRule ^ - [F]

selectively grab portion of query string from url via url rewriting in htaccess

I have this in my .htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=(?!admin)(?!superadmin)((?![^&]*?edit)[^\s&]+) [NC]
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
This rewrites in both directions. One way from:
domain.com/index.php?p=Home&l=en&m=0
to:
domain.com/Home
and vice versa. What I would like to do is preserve the query string part of the URL that comes after the value of 'p' and pass it along BOTH WAYS.
I was able to do one way, with this code:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=(?!admin)(?!superadmin)((?![^&]*?edit)[^\s&]+) [NC]
RewriteCond %{THE_REQUEST} /index\.php\?p=([^\s&]+)&p=([^\s&]+) [NC]
RewriteRule ^ /%1?%2? [R=301,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]
but I can't get the other way right. Can you guys help? I basically want the value of p to be written as part of the URL after the domain and I don't want it to reappear again but I want all the other variables to be appended.
Right now what I get is:
domain.com/Home?m=0?&p=Home&ms=m0&l=en
This should work for you:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?p=((?!(?:admin|superadmin|[^&]*?edit))[^\s&]+)(?:&(\S+))? [NC]
RewriteRule ^ /%1?%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L,QSA]

.htaccess multi RewriteRule and RewriteCond help me to combine them together

I need help with the following .htaccess, i need to combine them together if its possible
This will redirect to the domainname.com.au with http://www at the front of domain name if there is no http:// or www at the front:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domainname.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domainname.com.au$
RewriteRule ^(.*)$ "http\:\/\/www\.domainname.com.au\/$1" [R=301, OR]
And this one should get the variables that passing at the end of the domain name after slash /, I should see "/contact" but the php MVC see this "/index.php?url=contact"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
deny from 111.93.163.217
Thanks!
The two rules can't be combined as they're quite different, but they can be shortened:
# Force www
RewriteCOnd %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Rewrite parameter
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [B,NE,QSA,L]

.htaccess make subdirectory root for domain

I am trying to use htaccess to make a subdirectory the root for an additional domain. I currently have:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?thedrive.co$
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/$1
RewriteCond %{HTTP_HOST} ^(www.)?thedrive.co$
RewriteRule ^(/)?$ test/index.php [L]
It works great and hides the 'test' directory in the URL. But, when a file in a subdirectory is requested it shows the 'test' directory. For example, instead of thedrive.co/somefolder it shows thedrive.co/test/somefolder.
How can I get it to not display that?
Insert a 301 rule right at the top below RewriteEngine on line:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?radioreformation\.co$ [NC]
RewriteCond %{THE_REQUEST} \s/+radioreformation.com/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?radioreformation.co$
RewriteCond %{REQUEST_URI} !^/radioreformation.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /radioreformation.com/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?radioreformation.co$
RewriteRule ^(/)?$ radioreformation.com/index.php [L]
This will redirect thedrive.co/test/somefolder to thedrive.co/somefolder

Redirecting site to sub-sub folder

I have a website in my public_html folder placed in the following folder structure
public_html/dps/main
I am using .htaccess to redirect in the following format ;
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?website.co.uk$
RewriteCond %{REQUEST_URI} !^/dps/main/
RewriteCond /domain/%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /dps/main/$1/
RewriteCond %{HTTP_HOST} ^(www.)?website.co.uk$
RewriteCond %{REQUEST_URI} !^/dps/main/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dps/main/$1
RewriteCond %{HTTP_HOST} ^(www.)?website.co.uk$
RewriteRule ^(/)?$ /dps/main/index.php [L]
Unfortunately it gives me Internal Server Error
not sure what is wrong with this code.
Thank you for your help in advance
Replace your rules with this:
DirectoryIndex index.php
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?website\.co\.uk$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!dps/main/).*)$ /dps/main/$1 [L,NC]