Subject: Force the web visitor to a single out of different domains
This post is a translation of what can be found here: http://dotforward.de/tipps-nowww.php
This kind of redirection can be used to have requests to a website that's available under different domains point to a single domain. So for example if the website can be reached with www.mydomain.com and www.myotherdomain.com you can automatically redirect the visitor to mydomain.com, including all direct URLs in that domain. For mydomain.com, no redirection is required, obviously. Using just a single domain has the advantages that search engines have it easier crawling your site and you can avoid problems with cookies stored for a specific domain. Apart from that, there's a reason not to use the "www" subdomain anyway, but that's still up to you.
Put that in the file named .htaccess in your domain's root directory. The red marked domain "mydomain.com" must be replaced with your own preferred domain: Unchanged in the first line and with each dot "." replaced with "\." (regular expression) in the second line.
RewriteEngine On
# Enforce the use of a preferred hostname
# (see http://httpd.apache.org/docs-2.0/mod/mod_rewrite.html#rewr…
# and http://httpd.apache.org/docs-2.0/misc/rewriteguide.html )
#
# Set hostname
SetEnvIf Host .+ desired_hostname=mydomain.com
SetEnvIfNoCase Host ^mydomain\.com$ !desired_hostname
#
# SSL, port not 443
RewriteCond %{ENV:desired_hostname} !^$ [NC]
RewriteCond %{HTTPS} ^on$
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*) https://%{ENV:desired_hostname}:%{SERVER_PORT}/$1 [L,R=301]
# SSL, port 443
RewriteCond %{ENV:desired_hostname} !^$ [NC]
RewriteCond %{HTTPS} ^on$
RewriteRule ^(.*) https://%{ENV:desired_hostname}/$1 [L,R=301]
# Port not 80
RewriteCond %{ENV:desired_hostname} !^$ [NC]
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^(.*) http://%{ENV:desired_hostname}:%{SERVER_PORT}/$1 [L,R=301]
# Port 80
RewriteCond %{ENV:desired_hostname} !^$ [NC]
RewriteRule ^(.*) http://%{ENV:desired_hostname}/$1 [L,R=301]
Should there be problems with subdomains that are then redirected to subdirectories of the main domain, you can replace the red marked ".+" from the first line with the exact domain that you want to redirect away from, e.g. "www.mydomain.com". The syntax for that is ^www\.mydomain\.com$. To specify multiple domains, this line can be repeated for each of them respectively.
This kind of redirection can be used to have requests to a website that's available under different domains point to a single domain. So for example if the website can be reached with www.mydomain.com and www.myotherdomain.com you can automatically redirect the visitor to mydomain.com, including all direct URLs in that domain. For mydomain.com, no redirection is required, obviously. Using just a single domain has the advantages that search engines have it easier crawling your site and you can avoid problems with cookies stored for a specific domain. Apart from that, there's a reason not to use the "www" subdomain anyway, but that's still up to you.
Put that in the file named .htaccess in your domain's root directory. The red marked domain "mydomain.com" must be replaced with your own preferred domain: Unchanged in the first line and with each dot "." replaced with "\." (regular expression) in the second line.
RewriteEngine On
# Enforce the use of a preferred hostname
# (see http://httpd.apache.org/docs-2.0/mod/mod_rewrite.html#rewr…
# and http://httpd.apache.org/docs-2.0/misc/rewriteguide.html )
#
# Set hostname
SetEnvIf Host .+ desired_hostname=mydomain.com
SetEnvIfNoCase Host ^mydomain\.com$ !desired_hostname
#
# SSL, port not 443
RewriteCond %{ENV:desired_hostname} !^$ [NC]
RewriteCond %{HTTPS} ^on$
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*) https://%{ENV:desired_hostname}:%{SERVER_PORT}/$1 [L,R=301]
# SSL, port 443
RewriteCond %{ENV:desired_hostname} !^$ [NC]
RewriteCond %{HTTPS} ^on$
RewriteRule ^(.*) https://%{ENV:desired_hostname}/$1 [L,R=301]
# Port not 80
RewriteCond %{ENV:desired_hostname} !^$ [NC]
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^(.*) http://%{ENV:desired_hostname}:%{SERVER_PORT}/$1 [L,R=301]
# Port 80
RewriteCond %{ENV:desired_hostname} !^$ [NC]
RewriteRule ^(.*) http://%{ENV:desired_hostname}/$1 [L,R=301]
Should there be problems with subdomains that are then redirected to subdirectories of the main domain, you can replace the red marked ".+" from the first line with the exact domain that you want to redirect away from, e.g. "www.mydomain.com". The syntax for that is ^www\.mydomain\.com$. To specify multiple domains, this line can be repeated for each of them respectively.
♪ ...nanananah, all in all we’re just brilliant thieves, nanananah... ♪♬

Yves
Show profile
Link to this post