.htaccess several domains to point to different locations
I have a main domain in a hosting plan and then several domains "parked" in the same hosting plan:
main_domain.com
domain1.com
domain2.com
domain3.com
I need each of those domains to be MASK-redirected like this:
domain1.com/home --> main_domain.com/index.php?loc=1§=1
domain1.com/about --> main_domain.com/index.php?loc=1§=2
domain1.com/contact --> main_domain.com/index.php?loc=1§=3
...
domain2.com/home --> main_domain.com/index.php?loc=2§=1
domain2.com/about --> main_domain.com/index.php?loc=2§=2
domain2.com/contact --> main_domain.com/index.php?loc=2§=3
...
domain3.com/home --> main_domain.com/index.php?loc=3§=1
domain3.com/about --> main_domain.com/index.php?loc=3§=2
domain3.com/contact --> main_domain.com/index.php?loc=3§=3
...
When I say MASK redirected I mean that in the browser's URL shows:
domain1.com/home --> but it's loading --> main_domain.com/index.php?loc=1§=1
Can it be done with .htaccess?
Since all the domains point to the same filesystem, you don't need to think about the main_domain.com. Although if you did need this (for some back-end reason?) it complicates matters, as you can't simply rewrite to another domain, you would need to proxy the request.
Using mod_rewrite in .htaccess...
Verbose method:
RewriteEngine On
# domain1.com
RewriteCond %HTTP_HOST domain1.com [NC]
RewriteRule ^home$ index.php?loc=1§=1 [L]
RewriteCond %HTTP_HOST domain1.com [NC]
RewriteRule ^about$ index.php?loc=1§=2 [L]
RewriteCond %HTTP_HOST domain1.com [NC]
RewriteRule ^contact$ index.php?loc=1§=3 [L]
# Repeat for domain2.com
# :
# Repeat for domain3.com
# :
More concise Method:
This avoids the repetitive checks for the host before each rule.
RewriteEngine On
# Set an environment variable (LOC) according to which domain is accessed
RewriteCond %HTTP_HOST_1 domain1.com_(d+)$ [NC,OR]
RewriteCond %HTTP_HOST_2 domain2.com_(d+)$ [NC,OR]
RewriteCond %HTTP_HOST_3 domain3.com_(d+)$ [NC]
RewriteRule ^ - [E=LOC:%1]
# (Optional) If it's not one of the recognised domains then abort (403)
RewriteCond %ENV:LOC ^$
RewriteRule ^ - [F]
# Route the request for each domain
RewriteRule ^home$ index.php?loc=%ENV:LOC§=1 [L]
RewriteRule ^about$ index.php?loc=%ENV:LOC§=2 [L]
RewriteRule ^contact$ index.php?loc=%ENV:LOC§=3 [L]
The %1 in the first block is a backreference to whatever matched the captured group (ie. (d+)) in the last matched RewriteCond directive (if any). This is your location code, which is assumed to be numeric (as in your example). This is then assigned to the LOC environment variable (with [E=LOC:%1]).
RewriteCond %HTTP_HOST_1 domain1.com_(d+)$ [NC,OR]
For example, the 1 in %HTTP_HOST_1 is the location code for domain1.com. If %HTTP_HOST_ matches the regex domain1.com_ then the 1 is captured by (d+) and later assigned to the LOC environment variable.
If you could simply pass the domain name as the "location code" then you wouldn't need the first block. You could just pass %HTTP_HOST directly and your script would then need to handle it.
QUESTION: I used to be able to access the site via server IP (something like
203.0.113.111/~theultr5/index.php...) but it doesn't work now. What rule do I have to add to get this functionality back?
Normally you want to block access by the IP address (prevent duplicate content, etc.). However, one way is to make an exception for the IP address. Immediately after the RewriteEngine directive, try the following:
RewriteCond %HTTP_HOST =203.0.113.111
RewriteRule ^ - [L]
This will prevent any further processing if you access the site by the IP address. So, the /home, /about and /contact URLs will not function - you will need to specify the resulting URL yourself.
Comments
Post a Comment