I have a main domain in a hosting plan and then several domains "parked" in the same hosting plan:


I need each of those domains to be MASK-redirected like this:

domain1.com/home --> main_domain.com/index.php?loc=1&sect=1
domain1.com/about --> main_domain.com/index.php?loc=1&sect=2
domain1.com/contact --> main_domain.com/index.php?loc=1&sect=3

domain2.com/home --> main_domain.com/index.php?loc=2&sect=1
domain2.com/about --> main_domain.com/index.php?loc=2&sect=2
domain2.com/contact --> main_domain.com/index.php?loc=2&sect=3

domain3.com/home --> main_domain.com/index.php?loc=3&sect=1
domain3.com/about --> main_domain.com/index.php?loc=3&sect=2
domain3.com/contact --> main_domain.com/index.php?loc=3&sect=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&sect=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&sect=1 [L]
RewriteCond %{HTTP_HOST} domain1\.com [NC]
RewriteRule ^about$ index.php?loc=1&sect=2 [L]
RewriteCond %{HTTP_HOST} domain1\.com [NC]
RewriteRule ^contact$ index.php?loc=1&sect=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}&sect=1 [L]
RewriteRule ^about$ index.php?loc=%{ENV:LOC}&sect=2 [L]
RewriteRule ^contact$ index.php?loc=%{ENV:LOC}&sect=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 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} =
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.

  • OMFG!! You are a genius! That was exactly what I was looking for. I realized after some tests that the %{ENV:LOC} variable comes from the %{HTTP_HOST}_n part. It totally works!!! If you want to see the result you can check theultimatewomensexpo.com Each of the cities corresponds to the loc_id parameter. – Alvaro Mar 2 '17 at 16:59
  • Oops! if you want to see the result in the link above don't try Arizona, Los Angeles or Dallas (it's not implemented there yet). Thanks again! – Alvaro Mar 2 '17 at 17:09
  • QUESTION: I used to be able to access the site via server IP (something like but it doesn't work now. What rule do I have to add to get this functionality back? – Alvaro Mar 2 '17 at 17:44
  • Glad you got it working, sorry I kinda skimped on the explanation. I've updated my answer with a bit more explanation and answered your additional query about accessing the site by the IP address. – MrWhite Mar 2 '17 at 19:15
  • Impressive. Thank you so much again for your help. – Alvaro Mar 2 '17 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.