|htaccess 301 redirect - wordpress site|
| 1:53 pm on Oct 7, 2010 (gmt 0)|
I have the below in my htaccess
redirect 301 / http://www.newsite.com/
redirect 301 /about/ http://www.newsite.com/about/
redirect 301 /services/ http://www.newsite.com/our-services/
redirect 301 /contact/ http://www.newsite.com/contact-us/
The first redirect works fine. The second redirects fine, however the 3rd and 4th dont
As you can see the 3rd and 4th redirect are for inner pages being redirected to the new site inner pages with different URLs. i.e our-services not services
When I enter the old url, example
www.oldsite.com/services/ in the address bar, I get redirected to
www.newsite.com/services/ and not
Any ideas why this is, am I missing something?
| 2:37 pm on Oct 7, 2010 (gmt 0)|
That's because the first rule is the one that also matches requests for /services/ and for /contacts/ and re-appends the folder name in the redirect.
Change the order of the redirects, putting the most specific patterns first, or else use RewriteRule with more specific start and end anchored patterns instead of using Redirect.
| 2:54 pm on Oct 7, 2010 (gmt 0)|
Hi mate, thanks for the reply.
redirect 301 /index.php http://www.newsite.com/ work?
I am not very experienced with 301 redirects etc so I appreciate any help you can offer.
Can you give me an example of using the rewrite rules?
| 3:14 pm on Oct 7, 2010 (gmt 0)|
The basic problem is that the "Redirect" directive uses "prefix-matching" and therefore, your first Redirect will redirect all requests for *anything* on this server to the same "thing" on the other server. Having done that, none of the following Redirect directives will ever be processed.
The simple solution is to order your rules with the most-specific ones first:
Redirect 301 /services/ http://www.newsite.com/our-services/
Redirect 301 /contact/ http://www.newsite.com/contact-us/
Redirect 301 / http://www.newsite.com/
And note that because oldsite.com/about/ is to be redirected to newsite.com/about/ with no change to the page-name, this case will be taken care of by the third Redirect directive, so a fourth will not be needed.
See Apache mod_alias documentation for information on the different URL-path matching methods implemented by the Redirect and RedirectMatch directives.
| 3:27 pm on Oct 7, 2010 (gmt 0)|
Do I need anything at the start of the file or can I go straight into it as listed?
| 3:38 pm on Oct 7, 2010 (gmt 0)|
Edit: Jim's solution is fine as well, and a more direct answer to your question; we both posted our answers at the same time with slightly different approaches...
Check the library for this forum (linked at the top) for Rewrite examples.
I would recommend that you take the time to write down the full problem you want to solve, with some examples of the particular cases that you need to handle. This is mostly for your benefit (it's a tech specification, really), but you can put it up here after you try things out and see what is not working.
The charter of the forum is to help people work through specific problems. The previous response correctly says that you have to do specific cases first, since these things (Redirect, which is the old way, or RewriteRule, which is more powerful and common) are processed in order.
That said, here's a start NOTE: THIS IS AN EXAMPLE WHICH MAY OR MAY NOT APPLY TO YOUR CASE
file .htaccess in DocumentRoot of your server
# Any specific cases where old path does not match new one
RewriteRule ^services/$ http://www.newsite.com/our-services/ [R=301,L]
RewriteRule ^contact/$ http://www.newsite.com/contact-us/ [R=301,L]
RewriteRule ^index.php$ http://www.newsite.com/ [R=301,L]
# General rewrite for everything else whose new path is same as old
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301]
(What happens now if someone goes to
http://www.oldsite.com/services (without the trailing slash)? Nice websites or servers sometimes add the slash -- if so, the first rules could be written in this form:
RewriteRule ^services/?$ http://www.newsite.com/our-services [R=301,L]
The ? is part of the regular expression pattern that says "the preceding character is optional"
Hope this gets you started. But it's up to you to test, and make sure it works and check your log files for errors, etc. Getting this wrong could mess up your site's ranking in Google, if that's something that you care about :-)