Forum Moderators: phranque

Message Too Old, No Replies

Different rewriterule for root and domain

         

123design

2:11 pm on Mar 31, 2011 (gmt 0)

10+ Year Member



Hello,

I have a rewriterule for different domains.
But I can't set for each domain a different root location.
I must configure a specific root location and for other request I use the other location.



RewriteCond %{HTTP_HOST} ^www.domain1.com$
RewriteCond %{REQUEST_URI} !^/$

RewriteRule ^(.*)$ web/modx1/$1
RewriteRule ^$ test1.html [L]

RewriteCond %{HTTP_HOST} ^www.domain2.com$
RewriteCond %{REQUEST_URI} !^/$

RewriteRule ^(.*)$ web/modx2/$1
RewriteRule ^$ test2.html [L]

RewriteCond %{HTTP_HOST} ^www.domain3.com$
RewriteCond %{REQUEST_URI} !^/$

RewriteRule ^(.*)$ web/modx3/$1
RewriteRule ^$ test3.html [L]

jdMorgan

11:17 pm on Apr 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I do not know what the second rule of each set is intended to accomplish other than to rewrite requests for the "root" URL-path to pages named "test". The following rules will first redirect to add "www" to each hostname if it is missing, and then internally rewrite each recognized hostname to its own subdirectory unless this rewrite has already been done (in .htaccess, recursion must be explicitly prevented).

# Externally redirect non-www hostname requests to canonical www hostnames
RewriteCond %{HTTP_HOST} ^(domain1\.com) [NC,OR]
RewriteCond %{HTTP_HOST} ^(domain2\.com) [NC,OR]
RewriteCond %{HTTP_HOST} ^(domain3\.com)
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
#
# Internally rewrite recognized hostname to appropriate subdirectory (unless already done)
RewriteCond $1 !^web/(modx1|modx2|modx3)/
RewriteCond %{HTTP_HOST}>modx1 ^www\.domain1\.com[^>]*>(.+)$ [OR]
RewriteCond %{HTTP_HOST}>modx2 ^www\.domain1\.com[^>]*>(.+)$ [OR]
RewriteCond %{HTTP_HOST}>modx3 ^www\.domain1\.com[^>]*>(.+)$
RewriteRule ^(.*)$ /web/%1/$1 [L]

The ">" character is arbitrary, and is used here only as a delimiter to disambiguate and speed up regex processing.

The somewhat-complex hostname-matching pattern is needed because "www.example.com.:80" is a perfectly-valid and possible HTTP_HOST value, so the FQDN tokens and port numbers must be taken into account in the regex pattern.

Jim