Forum Moderators: phranque

Message Too Old, No Replies

htaccess for domain name change

         

8PDesign

4:34 pm on Apr 23, 2010 (gmt 0)

10+ Year Member



Hi, I need help for a particular htaccess problem

We had a domain (olddomail.com), but we want everything to point to new one. Many pages are indexed on SE (olddomail.com/pagename.php). So we want to keep the links.. but don't want to redefine them all.

We want to stop using the olddomain.com and move everything to newdomain.com

So we want olddomail.com/<anypath>/pagename.php
to redirect to olddomail.com/<anypath>/pagename.php

thanks!

jdMorgan

8:20 pm on Apr 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please see the resources cited in our Apache Forum Charter and the tutorial and example threads posted in our Apache Forum Library for a good start.

Then post your best-effort code as a basis for discussion.

Thanks,
Jim

8PDesign

8:52 pm on Apr 23, 2010 (gmt 0)

10+ Year Member



I tried the following and it does not keep the page:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RewriteCond %{HTTP_HOST} ^olddomain.ca(.*)$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.ca(.*)$
RewriteRule ^(.*)$ http\:\/\/www\.newdomain.ca\/$1 [R=301,L]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

thanks for your help!

g1smd

12:26 am on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The [OR] can be improved; only one line is needed.

The trailing (.*) isn't required.

There's no need to escape the literal target URL.

You do need to escape the literal periods in the pattern.

It should have worked but this is more simple:

RewriteCond %{HTTP_HOST} !^(w[i][/i]ww\.newdomain\.ca)?$
RewriteRule (.*) ht[i][/i]tp://w[i][/i]ww.newdomain.ca/$1 [R=301,L]


"Redirect if the requested domain is not EXACTLY www.newdomain.ca" and that takes care of non-www and all old domain requests.

8PDesign

8:20 pm on Apr 26, 2010 (gmt 0)

10+ Year Member



but now
www.olddoamin.ca/PageToVisit.php
does not redirect to www.newdomain.ca/PageToVisit.php
it stays at www.olddoamin.ca/PageToVisit.php

It seems to only work if I call the domain without a page.
www.olddoamin.ca now redirects to www.newdomain.ca

But not if I have a page call.

thanks again!

g1smd

8:41 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Something else must be interfering with the redirect. It's a simple redirect, two lines of code.

Did you flush your browser cache before testing again?

8PDesign

8:52 pm on Apr 26, 2010 (gmt 0)

10+ Year Member



I don't have any cache (disabled)
And I just cleared everyhting but this in the htaccess

RewriteEngine on

RewriteCond %{HTTP_HOST} !^(www\.old\-domain\-one\.ca)?$
RewriteRule (.*) http\:\/\/www\.new\-domain\-two\.ca\/$1 [R=301,L]

g1smd

10:19 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No escaping is needed for hyphens in the pattern nor anywhere in the target URL.

Page name is preserved in this redirect.

jdMorgan

11:19 pm on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That code will only work if the request is NOT for old-domain-one. Here's a correction with detailed comments:

# Enable the rewriting engine
RewriteEngine on
#
# If the requested hostname is NOT ( *exactly* "www.new-domain-two.ca" or blank )
RewriteCond %{HTTP_HOST} !^(www\.new-domain-two\.ca)?$
# then redirect the client to www.new-domain-two.ca, preserving the original URL-path and query
RewriteRule ^(.*)$ http://www.new-domain-two.ca/$1 [R=301,L]

All escaping and anchoring above is in accordance with mod_rewrite's requirements and best practices.

I believe you missed the meaning of "!" -- which is a unary negation operator when present preceding mod_rewrite patterns...

Jim

8PDesign

11:33 pm on Apr 26, 2010 (gmt 0)

10+ Year Member



ok, I'm fighting this and can't resolve. Sorry but I will post full htaccess below with real urls

# Enable the rewriting engine
RewriteEngine on
#
# If the requested hostname is NOT ( *exactly* "www.example.ca" or blank )
RewriteCond %{HTTP_HOST} !^(www\.example\.ca)?$
# then redirect the client to www.example.ca, preserving the original URL-path and query
RewriteRule ^(.*)$ http://www.example.ca/$1 [R=301,L]

if I call http://example-two.ca/psychotherapie.php
I would like to get http://www.example.ca/psychotherapie.php

what I am doing wrong?!
thanks!

[edited by: jdMorgan at 1:03 am (utc) on Apr 27, 2010]
[edit reason] example.com [/edit]

jdMorgan

1:11 am on Apr 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code was fine, except that we don't allow "real URLs" to be posted here. This is for everybody's protection, including yours.

Maybe you're not doing anything wrong in the code itself.

Where are you putting this code -- what is its URL or filepath? Is it in a directory location that will be accessed when a request is made for the non-canonical domain?

Is the non-canonical domain pointed to this server in your DNS record? Is this hostname pointed to the same DocumentRoot as the canonical domain?

Could be any one of dozens of things wrong here, and we can only guess at a few of the most likely ones...

Jim