Welcome to WebmasterWorld Guest from 34.204.189.171

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

New Domain Redirection

htaccess newbie question

     
3:12 pm on Sep 19, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 13, 2006
posts: 125
votes: 0


I just changed domains from example1.com to example2.com. How would I make it so that if someone goes to example1.com/somepage.htm that they are automatically redirected to example2.com/somepage.htm?

Is htaccess the way to do this?

Thank you in advance!
4:21 pm on Sept 19, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15874
votes: 872


Not enough information. Did you change anything besides your domain name? New host? New server? Before you start pulling out your hair, see if your host will do it for you. (It's like the www redirect: anything that keeps people from messing about with htaccess is probably to the host's advantage ;))
4:34 pm on Sept 19, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 13, 2006
posts: 125
votes: 0


Same host, same hosting account with 2 domain names pointing to the same IP. The hosting company said to do it through htaccess and pointed me to a generic page that didn't describe what I need to do. They don't want to do anything with htaccess.

There may be a better way to do it but I'm not sure what it is. I'm open to any suggestions.
9:39 pm on Sept 19, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15874
votes: 872


First suggestion: search this forum for previous posts describing the identical situation. The question comes up several times a week, so the answers have gotten shorter and shorter ;)
9:53 pm on Sept 19, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 13, 2006
posts: 125
votes: 0


Well I did look through some posts but I'm not sure what it is I'm looking for exactly so I don't know if I've found what I need.
10:00 pm on Sept 19, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Check out a few of these previous posts. Some are very detailed, others less so.

[google.com...]

This type of question has been asked and answered more than 1000 times in the last 10 years.

Use a RewriteRule with a preceding RewriteCond looking at HTTP_HOST. You want the ruleset where you redirect any hostname request that is not "exactly" www.example2.com to www.example2.com keeping the originally requested path in the redirected request. Use the [R=301,L] flags on the end of the rule.

[edited by: g1smd at 10:08 pm (utc) on Sep 19, 2011]

10:06 pm on Sept 19, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 13, 2006
posts: 125
votes: 0


Thank you, I will look through there and see if I can find what I'm looking for.
2:52 pm on Sept 20, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 13, 2006
posts: 125
votes: 0


Ok so here is what I *think* I wrote:

If the domain is not example2.com redirect to www.example2.com/SomePage

If the domain is not www.example2.com, redirect to www.example2.com/SomePage

If the domain is example2.com, redirect to www.example2.com/SomePage

Does it make sense to do it in that order and is the syntax below correct? Here is the entire htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#
#
RewriteCond %{HTTP_HOST} !^example2\.com$ [NC]
RewriteRule (.*) http://www.example2.com/$1 [R=301,L]
#
#
RewriteCond %{HTTP_HOST} !^www\.example2\.com$ [NC]
RewriteRule (.*) http://www.example2.com/$1 [R=301,L]
#
#
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
RewriteRule (.*) http://www.example2.com/$1 [R=301,L]
</IfModule>

# END WordPress
3:00 pm on Sept 20, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


If someone requests a non-canonical hostname, your rules will internally rewrite the request to the index.php file, and then the redirect to www will expose that filename back out on to the web as a new URL.

You must list redirects before rewrites. The order is CRUCIAL.

As for the redirects, you do not need three. You just need one.

Consider this (ignoring your first rule of three):
If the domain is not www.example2.com, redirect to www.example2.com/SomePage

If the domain is example2.com, redirect to www.example2.com/SomePage
So, the first rule (of those two) redirects if the requested host name is not www.example2.com. It will therefore redirect requests for example2.com - which is what you want.

Now the second rule (of two) will never run for example2.com requests, because the previous rule has already taken care of the request.

With the NC flag in place WWW.Example.COM will not be redirected to the proper lower case URL. The [NC] flag must be deleted. It interferes with correct operation.

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


If the requested host is not "exactly" www.example2.com then redirect to www.example2.com. This will redirect example1.com, example2.com, www.example1.com, www.example2.com:80, and anything else that is not "exactly" www.example2.com for the hostname.

The ( ) ? part of the code ensures that pure HTTP/1.0 requests (where a host header is not sent) do not cause an infinite redirect loop.

"RewriteEngine On" must appear just ONCE at the beginning of the rules, before the first redirect.

You do not want rewriting to silently fail if mod_rewrite is not installed. Delete the ifModule tags.

Use example.com in the forum to supress URL auto-linking.

[edited by: g1smd at 3:27 pm (utc) on Sep 20, 2011]

3:18 pm on Sept 20, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 13, 2006
posts: 125
votes: 0


Ok, I should of thought of the 'only needing one' part. Duh.

So you are saying this is what I need? I'm not sure what the code is in the beginning. That was included in the existing file along with the ifmodule tags so I had left it in. This is pretty much my first venture into htaccess.


# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#
#
RewriteCond %{HTTP_HOST} !^(www\.example2\.com)?$
RewriteRule (.*) http://www.example2.com/$1 [R=301,L]
# END WordPress


And THANK YOU for the help!
3:25 pm on Sept 20, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


If someone requests a non-canonical hostname, your rules will internally rewrite the request to the index.php file, and then the redirect to www will expose that filename back out on to the web as a new URL.

A request for example1.com/thispage will see the user redirected to www.example2.com/index.php which is not what you want.

You must list redirects before rewrites. The order is CRUCIAL.
3:47 pm on Sept 20, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 13, 2006
posts: 125
votes: 0


I think I understand what you are saying. What confused me is that they all start out with RewriteRule. But since my new redirect has [R=301,L] that makes it a redirection instead of a rewrite? So my first way would have shown example.com/index.php instead of example.com. Correct?


# BEGIN WordPress
RewriteEngine On
#
RewriteCond %{HTTP_HOST} !^(www\.example2\.com)?$
RewriteRule (.*) http://www.example2.com/$1 [R=301,L]
#
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#

# END WordPress
3:53 pm on Sept 20, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


RewriteRule with protocol and hostname in the rule target (whether or not there are any flags) makes it a 302 redirect.

RewriteRule with [R,L] flags (whether or not the target contains protocol and domain) makes it a 302 redirect.

RewriteRule with [R=301,L] flags makes it a 301 redirect. Best practice sees all redirects coded with protocol and domain name in the target AND the [R=301,L] flags.

RewriteRule with only a filepath in the target and only the [L] flag is a rewrite.

This is all in the Apache mod_rewrite manual. You should read it over and over again until it makes sense. It is baffling stuff to begin with.

[edited by: g1smd at 4:03 pm (utc) on Sep 20, 2011]

3:57 pm on Sept 20, 2011 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 13, 2006
posts: 125
votes: 0


Thank you very much for your help!