homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

mod rewrite to a subdirectory?

Msg#: 4250396 posted 6:18 pm on Jan 7, 2011 (gmt 0)

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?



Msg#: 4250396 posted 6:24 pm on Jan 7, 2011 (gmt 0)

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?


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

Msg#: 4250396 posted 2:16 pm on Jan 9, 2011 (gmt 0)

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.


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved