homepage Welcome to WebmasterWorld Guest from 54.204.182.118
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirect old domain to new domain
mkingsle

5+ Year Member



 
Msg#: 4178578 posted 2:38 pm on Jul 29, 2010 (gmt 0)

I have to move my website to a new domain and I am concerned about doing it smoothly. I understand that I need to change .htaccess file to redirect from the old to the new, but my concern lies in that I have an existing file with rewrite conditions in place that I want to keep intact. Not sure if I need to keep everything the same with the domain or do I use the new domain for the following:

DefaultType application/x-httpd-php

AddHandler x-httpd-php .php4
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php?\ HTTP/ [NC]
RewriteRule ^(.*)index.php?$ [olddomain.com...] [R=301,L]
RewriteCond %{HTTP_HOST} ^olddomain\.com
RewriteRule ^(.*)$ [olddomain.com...] [R=permanent,L]
rewriterule ^ama/([A-Za-z0-9]+)$ /includes/amazon.php?id=$1


Another question is where in the order of things do I put the following for the redirect to the new website? Or does order not matter?

RewriteCond %{HTTP_HOST} olddomain.com [NC]
RewriteCond (.*) [newdomain.com...] [R=301,L]

Thanks for any help on this. It is appreciated.

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4178578 posted 1:09 am on Jul 31, 2010 (gmt 0)

Change "olddomain" in your existing rules to "newdomain."

Put your new "catch-all" domain redirect rule ahead of your last rule -- that is, this new redirect will be the last external redirect, and then your last rule does internal rewrites only.

Jim

mkingsle

5+ Year Member



 
Msg#: 4178578 posted 2:37 pm on Aug 2, 2010 (gmt 0)

Thanks for the help JD. You are always of great service.

Does this rule look correct, by the way?

RewriteCond %{HTTP_HOST} olddomain.com [NC]
RewriteCond (.*) [newdomain.com] [R=301,L]
RewriteRule (.*) [newdomain.com...] [R=301,L]

or should I use:

RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ [newexample.com...] [R=301,L]

or can I skip the RewriteCond statements all together and just use:

RewriteRule (.*) [newdomain.com...] [R=301,L]

or

RewriteRule ^(.∗)$ [newdomain.com...] [R=301,L]

Seems like there are several ways to slice this onion.

Thanks in advance.

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4178578 posted 11:25 pm on Aug 2, 2010 (gmt 0)

All are flawed in one way or another. Try:

# Externally redirect all requests for non-blank non-canonical hostnames to canonical hostname
RewriteCond %{HTTP_HOST} ^!(newdomain\.com)?$
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

Jim

mkingsle

5+ Year Member



 
Msg#: 4178578 posted 8:55 pm on Aug 10, 2010 (gmt 0)

This is what I have thus far:

DefaultType application/x-httpd-php

AddHandler x-httpd-php .php4
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php?\ HTTP/ [NC]
RewriteRule ^(.*)index.php?$ [newsite.com...] [R=301,L]
RewriteCond %{HTTP_HOST} ^newsite\.com
RewriteRule ^(.*)$ [newsite.com...] [R=permanent,L]
RewriteCond %{HTTP_HOST} !^www\.newsite\.com
RewriteRule (.*) [newsite.com...] [R=301,L]
rewriterule ^ama/([A-Za-z0-9]+)$ /includes/amazon.php?id=$1


Which lines are you saying i should replace with the following:

# Externally redirect all requests for non-blank non-canonical hostnames to canonical hostname
RewriteCond %{HTTP_HOST} ^!(newsite\.com)?$
RewriteRule ^(.*)$ [newsite...] [R=301,L]


Thanks Jim

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4178578 posted 1:18 am on Aug 11, 2010 (gmt 0)

It should look like this:

DefaultType application/x-httpd-php
AddHandler x-httpd-php .php4
#
Options +FollowSymLinks
RewriteEngine on
#
# Externally redirect direct client requests for "/index.php" or
# "/index.php4" in any subdirectory to "/" in that same directory
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/\ ]+/)*index\.php4?\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)index\.php4?$ http://newsite.com/$1 [R=301,L]
#
# Externally redirect all requests for non-blank non-canonical hostnames to canonical hostname
RewriteCond %{HTTP_HOST} !^(newsite\.com)?$
RewriteRule ^(.*)$ http://newsite.com/$1 [R=301,L]
#
# Internally rewrite "/ama/<id>" URL requests to amazon script filepath, passing <id> value in query string
RewriteRule ^ama/([A-Za-z0-9]+)$ /includes/amazon.php?id=$1

Note the comments. They are every bit as important as the code. If these comments don't say exactly what you want to do, then the code is wrong...

Jim

mkingsle

5+ Year Member



 
Msg#: 4178578 posted 4:07 pm on Aug 16, 2010 (gmt 0)

Thanks for the help Jim. I had one last question.

It seems that you left out one of the condition/rules that I had in my .htaccess:

RewriteCond %{HTTP_HOST} ^newsite\.com
RewriteRule ^(.*)$ [newsite.com...] [R=permanent,L]

Was this because it was redundant and therefore combined with another in your final solution? Honestly, I can't remember what the above does.

p.s.

I've learned a valuable lesson for further websites...always use comments.

mkingsle

5+ Year Member



 
Msg#: 4178578 posted 4:15 pm on Aug 16, 2010 (gmt 0)

Hi again Jim.

I got an error when using your solution:

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

Thanks in advance.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4178578 posted 9:51 pm on Aug 16, 2010 (gmt 0)

The final rule needs an [L] flagged added for sure.

The missing rule appears to be catered for in other code.

Check the code carefully, do you want the www included or omitted in your target URL?

mkingsle

5+ Year Member



 
Msg#: 4178578 posted 2:08 pm on Aug 17, 2010 (gmt 0)

Hi Jim:

Basically what I want in my code is the following:

# Redirect direct client requests for /index.php in any directory to "/" in that directory

# Redirect direct client requests to remove .php extensions

# Redirect all non-blank non-canonical hostname requests to canonical hostname

# Redirect to new website from old website

# Redirect the /ama urls to the amazon.php file


The code I provided seems to work for everything I want and Google seems to be understanding. At this point, my rankings have not been affected too much (a couple spots down), which is pretty good thus far.

My initial question was whether my code was efficiently written. What I did was copy the code from your previous answer and pasted it into my htaccess file, making the changes to reflect my site name. I do want it to go to www versus without www.

I've always wondered why when you post code in this forum it gets messed up? Wish there was a way around that so people can share code verbatim. Maybe you posted the answer and it had characters in it that I am not to have in the code, like the "( )"? Just a guess.

Thanks for your help.

Michael

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4178578 posted 7:30 pm on Aug 17, 2010 (gmt 0)

Your original example code redirected to www.example.com but jd's example code redirected to example.com, without www, and that difference is crucial.

Check which one you want to redirect to and make sure the code you use is exactly right. It will take forever to fix things, should you implement code that redirects in the 'wrong direction'.

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4178578 posted 4:11 am on Aug 19, 2010 (gmt 0)

http://example.com and http://www.example.com are recognized as example domains and won't "get messed up" if you use them in posts. Otherwise, URLs will trigger the forum's auto-linking code, and you see the results...

URLs inside contiguous [ code ] sections will not be autolinked.

Where it is necessary outside of a [ code ] section to use other domains for clarity (such as http://newdomain.com and http://olddomain.com), you can 'break' the autolinking by including [bold] or [italic] bbCode pairs or by using the [ smilestopper ] code tags after the http: part of the URL.

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved