Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod rewrite to a subdirectory?



6:18 pm on Jan 7, 2011 (gmt 0)

5+ Year Member

I have a website, lets call it websitea.com and another domain I own websiteb.com. Well websiteb.com's CMS is actually installed inside of websitea.com/dubdir so can I use .htaccess to point all traffic for websiteb.com to websitea.com/subdir without the users ever knowing they are on the same server?


6:24 pm on Jan 7, 2011 (gmt 0)

5+ Year Member

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteRule ^(.*)$ subdir/$1 [L]

I found this code online and think it can apply to me. Any confirmation?


2:16 pm on Jan 9, 2011 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Or equivalently, but more robust:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com\.?(:[0-9]+)?$
RewriteCond $1 !^subdir/
RewriteRule ^(.*)$ /subdir/$1 [L]

You will need to be careful, though. Be sure to place any external redirects that you may require for "mysite.com" ("websiteb" in your example) into this same top-level .htaccess file, and place them ahead of this internal domain-to-subdirectory rewrite. You will also have to use RewriteRule directives for those external redirects.

Failing to do this, the result will be to "expose" your internal "/subdir" filepaths to clients (browsers and search engine robots), with resulting user confusion and corruption of the search results for this domain.

Also note that if you are planning to add more domains using this method at some future time, it will be far more efficient to group them all under a common directory to make maintenance much simpler:

RewriteEngine On
RewriteCond $1 !^add-ons/
RewriteCond %{HTTP_HOST} ^(www\.)?(mysite\.com)\.?(:[0-9]+)?$
RewriteRule ^(.*)$ /add-ons/%2/$1 [L]

Here, we store each add-on domain's files under /add-ons/<domain-name>/ and we get that <domain-name> part from the HTTP request itself.

Also, you really should add domain canonicalization redirect rules ahead of this code, to prevent "duplicate content" problems and so that it will be unnecessary to allow for both the "www" and "non-www" hostname cases in the hostname-to-subdirectory (and any other) rewrites.


Featured Threads

Hot Threads This Week

Hot Threads This Month