How can I use .htaccess to rewrite different domains to different sub-directories, masking the sub-directory?
I finally found a solution. The second first RewriteCond
in my above code was unnecessary, and theRewriteRule
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://%{HTTP_HOST}/$1/ [L]
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$ [NC]
RewriteCond %{REQUEST_URI} !site1/ [NC]
RewriteRule ^(.*)$ /site1/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?site2.com$ [NC]
RewriteCond %{REQUEST_URI} !site/ [NC]
RewriteRule ^(.*)$ /site2/$1 [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.
Related videos on Youtube
akTed
Updated on September 18, 2022Comments
-
akTed almost 2 years
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/
orsite2/
. My .htaccess below almost works. If the user goes to a subdirectory likesite2.com/test/
(notice the trailing slash /), it works great. But if the user leaves out the trailing slash it showssite2.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/$1 [L] RewriteCond %{HTTP_HOST} ^(www.)?site2.com$ [NC] RewriteCond %{REQUEST_URI} !site2/ [NC] RewriteRule ^(.*)$ /site2/$1 [L]
I've tried many things, from different online sources, but nothing works. One thing that almost worked (notice the trailing slash after
$1
):RewriteRule ^(.*)$ /site2/$1/ [L]
This works correctly when the filename is omitted -
site2.com/test/
- but if you go tosite2.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.
-
MrWhite over 11 yearsIs your site accessible from both the
www
subdomain and the bare domain? You should perhaps be forcing one or the other (as an external redirect) before your internal rewrites I would have thought. YourREQUEST_URI
line should probably match!^/sitec
rather than!sitec/
since the current pattern matches anywhere in the URI. Aresitea
,siteb
, etc. the real directories/domains? -
akTed over 11 yearsI edited my Q with the WordPress .htaccess. If you say I mean "internal rewrite", I'll take your word for it.:) Feel free to edit my Q or tell me what/where to change. All three are accessible with www and no www, but siteA and siteB both remove the www (something I never noticed till now). All 3 point to their respective subdirectories...'http: //siteA.com' points to [my public root]/sitea/, 'http: //siteB.com' points to [my public root]/siteb/, etc.
-
MrWhite over 11 yearsIf "siteA and siteB both remove the www" then there must be a redirection in the WP code itself, as nothing you've posted above will do this redirection (however, this would normally be done in .htaccess). The problem, however, is that the code you have posted looks OK-ish - it should work. There is certainly nothing here that could be triggering the redirection you are seeing. So, it would seem there is "something else" going on IMO?! Check the HTTP requests using Chrome's debugger tools or "Live HTTP Headers" extension (Firefox) to see if/what redirections are occurring.
-
akTed over 11 years@w3d I tried adding the
^
like you'd mentioned in your first comment, but it throws a 500 Internal Server Error. -
paulmorriss over 11 years@AKTed I've got your question migrated back here as this is the best place for it. I've also removed comments which give information which you've now put back into the question, so it's easer to follow the remaining comments. I hope we can get a solution!
-
akTed over 11 years@paulmorriss thank you, Funny, I was actually just in the middle of crafting a new question, omitting some of the irrelevant stuff. I'll reword this one instead
-
akTed over 11 years@paulmorriss or anyone else: This question doesn't seem to be getting any answers here in webmasters.se. At least in SO.se it received some views and w3d attempted to help in these comnments, but those comments are less applicable since I reworded and refocused the question to more clearly state my problem, omitting the irrelevant WordPress info I had in it before. It seems others must have overcame similar problems, but I don't know how to get more eyes on this question. Do you have any recommendations? Should this question be deleted and I repost (here or SO) with different wording?
-