Not logged in. · Lost password · Register
Forum: Support Installation, update and import RSS
Force the web visitor to a single out of different domains
Avatar
Yves (Administrator) #1
User title: UNB developer & webmaster
Member since Jan 2004 · 3814 posts · Location: Erlangen, Germany
Group memberships: Administrators, Members
Show profile · Link to this post
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.
♪ ...nanananah, all in all we’re just brilliant thieves, nanananah... ♪♬
Avatar
Keith #2
User title: 漢字おたく
Member since Feb 2007 · 60 posts · Location: Japan
Group memberships: Members
Show profile · Link to this post
This is not working for me.  :-/
Avatar
Yves (Administrator) #3
User title: UNB developer & webmaster
Member since Jan 2004 · 3814 posts · Location: Erlangen, Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Can you give me some more details?
♪ ...nanananah, all in all we’re just brilliant thieves, nanananah... ♪♬
Avatar
Keith #4
User title: 漢字おたく
Member since Feb 2007 · 60 posts · Location: Japan
Group memberships: Members
Show profile · Link to this post
I pasted the code in and changed only two lines.

I tried it like this
    SetEnvIf Host ^www\.domain\.com$. desired_hostname=domain.com
    SetEnvIfNoCase Host ^domain\.com$ !desired_hostname


and this
    SetEnvIf Host ^www\.domain\.com$ desired_hostname=domain.com
    SetEnvIfNoCase Host ^domain\.com$ !desired_hostname

but it doesn't redirect.
What have I done wrong?
Avatar
Yves (Administrator) #5
User title: UNB developer & webmaster
Member since Jan 2004 · 3814 posts · Location: Erlangen, Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Did you try it with keeping the .+ first?
Are you sure you copied the first line: RewriteEngine on
♪ ...nanananah, all in all we’re just brilliant thieves, nanananah... ♪♬
Avatar
Keith #6
User title: 漢字おたく
Member since Feb 2007 · 60 posts · Location: Japan
Group memberships: Members
Show profile · Link to this post
Quote by Yves:
Did you try it with keeping the .+ first?

I hadn't tried it, but did just now and still not working.

Quote by Yves:
Are you sure you copied the first line: RewriteEngine on

I have that line in the file for the short urls. I am using short urls and they are still rewriting.
I have put the code inside  the <IfModule mod_rewrite.c> section.
I am using RewriteBase /
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
This board is powered by the Unclassified NewsBoard software, 20110527-dev, © 2003-2011 by Yves Goergen
Page created in 193.6 ms (99.7 ms) · 69 database queries in 135.3 ms
Current time: 2012-02-07, 19:58:38 (UTC +01:00)