.htaccess redirect of domain name alias to main domain but must show up as the alias domain

.htaccess redirect of domain name alias to main domain but must show up as the alias domain - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about domains, htaccess, apache, redirects, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


I have the following in a .htaccess file:



RewriteCond %HTTP_HOST ^www.aliasdomain.com$ [OR]
RewriteCond %HTTP_HOST ^aliasdomain.com$
RewriteRule ^(.*)$ http://www.maindomain.com/subdir1/subdir2/index.html [R]


In a browser I want aliasdomain.com to access the index.html file and show up as aliasdomain.com.



However it shows up as the full URL as follows:
http://www.maindomain.com/subdir1/subdir2/index.html


Solution :

You need to internally rewrite the request in order to keep aliasdomain.com in the browser, and not externally redirect as you are currently doing...



Remove the protocol/host from the RewriteRule substitution, to leave just a root-relative path, and remove the R (redirect) flag. The protocol/host in the substitution forces an external redirect.





RewriteCond %HTTP_HOST ^(www.)?aliasdomain.com$
RewriteRule ^$ /subdir1/subdir2/index.html [L]


The ^$ pattern in the RewriteRule directive ensures that only requests for the domain root (ie. http://aliasdomain.com/ or http://www.aliasdomain.com/) gets rewritten to the new URL, rather than http://aliasdomain.com/every/possible/url (as mentioned in the comments below).



Since aliasdomain.com is a domain alias of maindomain.com, the entire site is accessible via both domains. They point to the same place. This is what the above rewrite rules are dependent upon. You can't internally rewrite to a different host. So the above RewriteRule serves the file from aliasdomain.com.


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect