: How can I use .htaccess to rewrite different domains to different sub-directories, masking the sub-directory? I have a shared hosting plan that allows unlimited addon domains, but they can only
I have a shared hosting plan that allows unlimited addon domains, but they can only point to the public root (/). I use .htaccess to point different domains to their corresponding sub-directories. For example,
site1.com points to sub-directory /site1/
site2.com points to sub-directory /site2/
I don't want the user to see site1/ or site2/. My .htaccess below almost works. If the user goes to a subdirectory like site2.com/test/ (notice the trailing slash /), it works great. But if the user leaves out the trailing slash it shows site2.com/site2/test/ in the browser.
So my question boils down to, how can I mask the sub-directory whether or not the user types the trailing slash?
My root .htaccess looks like this (only with more domains, all in the same 3-lines per domain format)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$ [NC]
RewriteCond %{REQUEST_URI} !site1/ [NC]
RewriteRule ^(.*)$ /site1/ [L]
RewriteCond %{HTTP_HOST} ^(www.)?site2.com$ [NC]
RewriteCond %{REQUEST_URI} !site2/ [NC]
RewriteRule ^(.*)$ /site2/ [L]
I've tried many things, from different online sources, but nothing works. One thing that almost worked (notice the trailing slash after ):
RewriteRule ^(.*)$ /site2// [L]
This works correctly when the filename is omitted - site2.com/test/ - but if you go to site2.com/test/index.php it says "No input file specified."
The only file in root is .htaccess, everything else is in sub-directories.
Edit: Rewrote question, omitting irrelevant details.
More posts by @Turnbaugh106
1 Comments
Sorted by latest first Latest Oldest Best
I finally found a solution. The second RewriteCond in my above code was unnecessary, and the first RewriteRule below, found at B&T's Tips & Scripts, adds a trailing slash if none exists (unless it's a file, like index.php). Here's my new .htaccess. Notice the trailing slash directive only needs to be applied once.
.htaccess
RewriteEngine On
#ADD TRAILING SLASH TO DIRECTORY IF NONE EXISTS
RewriteRule ^([^.]+[^/])$ %{HTTP_HOST}// [L]
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$ [NC]
RewriteCond %{REQUEST_URI} !site1/ [NC]
RewriteRule ^(.*)$ /site1/ [L]
RewriteCond %{HTTP_HOST} ^(www.)?site2.com$ [NC]
RewriteCond %{REQUEST_URI} !site/ [NC]
RewriteRule ^(.*)$ /site2/ [L]
Edit
I'd orignally removed the second RewriteCond from each triplet above, and it worked for my main site + a couple of others. I didn't fully test, and later found a couple other domains I used infrequently weren't working (giving 500 internal errors). I added the second RewriteRule back in and they work again.
What's weird is, after I ended up deleting one of my working testing domain sub-directories and recreating it, the rewrites (without the second RewriteCond) stopped working (throwing the 500 errors). Adding the second RewriteCond made it work again.
Oh, well, I guess I'll chalk it up to the magic of mod_rewrite...as long as it's working.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.