.htaccess rewrite rules for multiple sites - regex

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]

Related

Modify code htaccess to accept parameters via URl

Modify code to accept parameters via URl. Rules to redirect URLs to www.site.com/acessorios.php tried are as follows, where .htaccess rules file is present along side with app folder.
RewriteEngine ON
RewriteBase /app/views/
##External redirect rules from here.
RewriteCond %{THE_REQUEST} \s/app/views/([^.]*\.php)\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite rules from here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]*\.php)/?$ /app/views/$1 [QSA,L]
Samples:
www.site.com/accessories.php?id=1
www.site.com/accessories.php?id=1&name=Tiago
www.site.com/accessories.php?id=1&name=Tiago&image=foto.png
Samples:
www.site.com/accessories/1
www.site.com/accessories/1/Tiago
www.site.com/accessories/1/Tiago/foto.png
This order may be different
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /app/views/
##External redirect rules from here....
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d+)&name=(\S+)\s [NC]
RewriteRUle ^ /%1/%2/%3? [R=301,L]
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\d+)&name=(\S+)&image=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
##Internal rules from here onwards....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /app/views/$1.php?id=$2&name=$3&image=$4 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /app/views/$1.php?id=$2&name=$3 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ /app/views/$1.php?id=$2 [QSA,L]

htaccess rules for domain mapping and preserve current configuration

I have a site with dynamic subdomains based on registered usernames that are parsed using php and for that my htaccess works without a problem.
Here are some example on how the redirect works from $_SERVER
thecspace.com/blogs
["QUERY_STRING"] => string(21) "template=www&q=blogs/"
http://www.thecspace.com/blogs/test-pp-blog-2904
["QUERY_STRING"] => string(38) "template=www&q=blogs/test-pp-blog-2904"
http://danvlad.thecspace.com/blogs/test-pp-blog-2904
["QUERY_STRING"] => string(47) "template=danvlad&q=blogs/test-pp-blog-2904"
CURRENT HTACCESS
RewriteCond %{HTTP_HOST} ^thecspace\.com$ [NC]
RewriteRule ^(.*)$ http://www.thecspace.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)?\.thecspace\.com
RewriteRule ^$ index.php?template=%2 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)?\.thecspace\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?template=%2&q=$1 [L,QSA]
Now comes the hard part. I want to allow custom domains to use the same rules. Let's say devdemogroup have the domain devdemogroup.com. Once they change DNS to point to domain.com I want to be able to allow that domain to use current code from domain.com
So, in theory a request to www.danvlad.com would be a this query string
["QUERY_STRING"] => string(47) "template=danvlad"
and www.danvlad.com/blogs/
["QUERY_STRING"] => string(47) "template=danvlad&q=blogs"
I've tried this but it won't work
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.thecspace\.com$ [NC]
RewriteRule ^$ /index.php?template=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.thecspace\.com$ [NC]
RewriteRule ^$ /index.php?template=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.com$ [NC]
RewriteRule ^$ /index.php?template=%1 [L]
RewriteRule ^(.*)$ /index.php?template=%1?q=$1 [L,QSA]
Can anyone help?
Thank you
Actually your last RewriteRule is without conditions, use it like this:
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.thecspace\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?template=%1?q=$1 [L,QSA]
Final solution
Route53
thecspace.com Zone
danvlad .thecspace.com CNAME www.thecspace.com
www.danvlad .thecspace.com CNAME www.thecspace.com
danvlad Zone
www.danvlad.com CNAME www.thecspace.com
Options FollowSymLinks
AllowOverride All
Allow from all
DirectoryIndex index.php
# Redirect thecspace.com and cspace.thecspace.com to www.thecspace.com
RewriteCond %{HTTP_HOST} ^thecspace\.com$ [NC] [OR]
RewriteCond %{HTTP_POST} ^cspace.thecspace.com$ [NC]
RewriteRule ^(.*)$ http://www.thecspace.com/$1 [R,L,QSA]
# Redirect abc.thecspace.com to www.thecspace.com/index.php?template=abc&q=zzz
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.thecspace\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?template=%1&q=$1 [L]
# Redirect www.abc.thecspace.com to www.thecspace.com/index.php?template=abc&q=zzz
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.thecspace\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?template=%1&q=$1 [L]
# Redirect www.abc.com to www.thecspace.com/index.php?template=abc&q=zzz
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?template=%1&q=$1 [L]
# Redirect www.thecspace.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME) !^$
RewriteRule ^(.*)$ /index.php?q=$1 [L]
Maybe it helps someone.

Redirect to internal web-directory

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]

.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

Virtual Subdomains

I want to redirect wildcard subdomains to the file "user_site.php" like the following :
RewriteCond $1 !^(user_site\.php|home_site\.php
RewriteCond %{HTTP_HOST} !^(www|mail|ftp)?\.?domain.com [NC]
RewriteRule ^(.*)$ /user_site.php/$1 [L]
but if the subdomain is www or empty , i want to rewrite it to "home_site.php"
how can this be done ?
Thanks
I've found it already answered here on stackoverflow , Thanks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^(.*)/?$ home_site.php/$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} ^/index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www|mail).domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+).domain.com$ [NC]
RewriteRule ^(.*)/?$ user_site.php/$1 [NC,QSA,L]