Forum Moderators: phranque

Message Too Old, No Replies

multiple domains in one hosting account - htaccess 301 redirect

multiple domains in one hosting account - htaccess 301 redirect

         

jabz

9:14 pm on Jun 11, 2007 (gmt 0)

10+ Year Member



Hi,

I have some problem, and could not find an answer online. I always used an .htaccess for redirecting, but it is not working properly with my new hoster. So, I have a problem.

I have various domains, pointing in one hosting account all on the same website. Now I want to use only one of the domains to be the official domain, with www. Si I did this in my .htaccess:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.maindomain.com$ [NC]
RewriteRule ^(.*) [maindomain.com...] [L,R=301]

So far, so good. It`s working. But now I want all the other domains also to redirect into this maindomain. Means: The user enters one of my domain names and gets redirected (301) to the www.- version of my maindomain.com

Does anybody know what I have to do, to make it work?!
Let me know, thanks! Your help is very much apprechiated.

Jab

jdMorgan

2:29 am on Jun 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Turn the problem upside down, and redirect all requests for hostnames that are not maindomain.com to maindomain.com:

RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^maindomain\.com
RewriteRule (.*) http://maindomain.com/$1 [R=301,L]

The first RewriteCond is only needed if your server is accessible via HTTP/1.0. True HTTP/1.0 requests do not include the hostname header, so this RewriteCond prevents (big) problems if the request is HTTP/1.0 and the hostname is blank. You can remove this line if you are on a shared name-based server, since such servers cannot be reached with a blank hostname.

Jim

jabz

5:31 am on Jun 12, 2007 (gmt 0)

10+ Year Member



Hi JD. That was a good idea. Thanks.
Only one problem remains, and I don`t get it fixed: I want my maindomain only work with www.

Currently, with the script so far, I`m redirecting to main.com, without www.

Thanks for your help.
Jab

jdMorgan

2:44 pm on Jun 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This code is very simple. It says, "If requested hostname is NOT blank and NOT 'abc' then redirect to 'abc'."

As long as the hostnames in both lines are the same, it will work as expected. Since the hostname in the RewriteCond is a regular-expressions pattern, the periods need to be escaped as shown, but otherwise, the hostnames in the two lines are identical.

So add the required "www" to both parts of the rule:


RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com
RewriteRule (.*) http://www.maindomain.com/$1 [R=301,L]

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim