Forum Moderators: phranque
Two questions:
When someone types in www.domain2.com/something (with a subdirectory), then they are redirected to www.maindomain.com/something. I am wanting it to stay at domain2.com.
Secondly, with the code below, am I doing everything correctly, are there any ways to fix it up or change it to make it better? I want to avoid trailing slash problems and be able to easily host multiple domains on my server.
Thanks for the help!
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
#Send domain2.com to subdirectory /domain2/
RewriteCond %{HTTP_HOST} ^(ww+\.)?domain2\.com
RewriteCond %{REQUEST_URI}!/domain2/
RewriteRule ^(.*)$ /domain2/$1 [L]
#Send domain3.com to subdirectory /domain3/
RewriteCond %{HTTP_HOST} ^(ww+\.)?domain3\.com
RewriteCond %{REQUEST_URI}!/domain3/
RewriteRule ^(.*)$ /domain3/$1 [L]
RewriteCond %{HTTP_HOST}!^www\.maindomain\.com [NC]
RewriteCond %{HTTP_HOST}!^www\.domain3\.com [NC]
RewriteCond %{HTTP_HOST}!^domain3\.com [NC]
RewriteCond %{HTTP_HOST}!^www\.domain2\.com [NC]
RewriteCond %{HTTP_HOST}!^domain2\.com [NC]
RewriteCond %{SERVER_PORT}!^443$
RewriteRule (.*) [maindomain.com...] [R=301,L]
</IfModule>
When someone types in www.domain2.com/something (with a subdirectory), then they are redirected to www.maindomain.com/something. I am wanting it to stay at domain2.com.
I believe this redirection is taking place because any request which is not rewritten by the first two rulesets will fall through to the third ruleset, where any requests not made to port 443 will be redirected to your main domain. It's not clear what you intended to do with that test for port 443, so I can't offer any advice.
Secondly, with the code below, am I doing everything correctly, are there any ways to fix it up or change it to make it better? I want to avoid trailing slash problems and be able to easily host multiple domains on my server.
Jim
Combining the code technique in the thread I cited above with your main domain redirect, we'd get:
# IF hostname header is non-blank
RewriteCond %{HTTP_HOST} .
# And IF not main domain
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com [NC]
# (get domain name from requested host header)
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.com
# And IF a subdirectory by that name does not exist
RewriteCond /%2 !-d
# AND IF not port 443
RewriteCond %{SERVER_PORT} !^443$
# Then externally redirect to main domain
RewriteRule (.*) http://www.maindomain.com/$1 [R=301,L]
#
# Internally rewrite <domain>.com/<path> to <domain>.com/<domain>/<path> except for main domain
# IF hostname header is non-blank
RewriteCond %{HTTP_HOST} .
# And IF not main domain
RewriteCond %{HTTP_HOST} !^(www\.)?maindomain\.com [NC]
# Extract (required) domain (%2), and first path element (%4), discard port number if present (%3)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^(www\.)?([^.]+)\.com(:[0-9]{1,5})?<>/([^/]+) [NC]
# Rewrite only when domain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %2<>%4 !^([^<]+)<>\1$ [NC]
# Rewrite /path to /domain/path
RewriteRule ^(.*) /%1/$1 [L]
Because my time is limited, I haven't tested this code, but it's designed to do what you want, and to allow you to add as many secondary domains as you like without changing the code. As long as you create a subdirectory to host the additional domains, those domains will be accepted. Otherwise, the request will be redirected to your main domain.
I have used the words "rewrite" and "redirect" carefully. A rewrite is internal to the server and will not update the browser address bar. A redirect is external, and will update the browser address bar, because the browser is involved in the transaction. If an internal rewrite appears to update the address bar, it is because of an error in the code or because a subsequent redirect is also invoked.
I think you can probably tell why the reply took awhile... ;)
Jim
I made have made a mistake. I see that now my subdirectories have to be named the same as the domain.
I tried making this change. Do the subdirectories have to be named *.com or whatever or just the main address? Like for www.domain2.com just domain2 correct? If so yeah it is just sending these requests to maindomain.
Thanks
# IF hostname header is non-blank
RewriteCond %{HTTP_HOST} .
# And IF not main domain
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com [NC]
# (get domain name from requested host header)
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.com
# And IF a subdirectory by that name does not exist
RewriteCond [b]%{DOCUMENT_ROOT}/[/b]%2 !-d
# AND IF not port 443
RewriteCond %{SERVER_PORT} !^443$
# Then externally redirect to main domain
RewriteRule (.*) http://www.maindomain.com/$1 [R=301,L]
#
# Internally rewrite <domain>.com/<path> to <domain>.com/<domain>/<path> except for main domain
# IF hostname header is non-blank
RewriteCond %{HTTP_HOST} .
# And IF not main domain
RewriteCond %{HTTP_HOST} !^(www\.)?maindomain\.com [NC]
# Extract (required) domain (%2), and first path element (%4), discard port number if present (%3)
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^(www\.)?([^.]+)\.com(:[0-9]{1,5})?<>/([^/]+) [NC]
# Rewrite only when domain not equal to first path element (prevents mod_rewrite recursion)
RewriteCond %2<>%4 !^([^<]+)<>\1$ [NC]
# Rewrite /path to /domain/path
RewriteRule ^(.*) /%1/$1 [L]
I found out last night that not all servers will support the "comparision" operation used in that code. The problem is that the "\1" local back-reference is a posix 1003.2 regular expressions construct, and not all operating systems have upgraded to posix 1003.2. I know it works under FreeBSD, and does not work under Unix.
So that means you'll have to hard-code the names of your add-on domains in the code. :(
Try hard coding just one subdomain name, and if that fixes the Server Error, then you'll know.
Jim
I simply have folders inside my root directory which are used as content for the other domains which have totally seperate domain names.
www.maindomain.com
www.seconddomainname.com (reads from certain folder)
etc.
See what I mean?
Thanks.
# Make sure we haven't already rewritten to an alternate domain's subdirectory
RewriteCond $1!^(domain1圬omain2圬omain3圬omain4)/
# Otherwise, extract requested domain name from HTTP_HOST request header
RewriteCond %{HTTP_HOST} ^(www\.)?(domain1圬omain2圬omain3圬omain4)\.com
# Rewrite to requested page in appropriate subdirectory
RewriteRule ^(.*) /%1/$1 [L]