Mobile app version of vmapp.org
Login or Join
Courtney195

: .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

@Courtney195

Posted in: #Domains #Htaccess #ModRewrite #Redirects

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&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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Courtney195

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

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 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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme