Welcome to WebmasterWorld Guest from 54.196.232.162

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

A tricky htaccess redirect issue

can't work this one out

     
11:30 pm on Apr 29, 2013 (gmt 0)

New User

10+ Year Member

joined:Aug 17, 2004
posts:35
votes: 0


I've recently switched a site to a new domain.

Most of the pages on the new domain have kept the same page.html names as they had on the old domain.

The following lines in the .htaccess file of the old domain do all that's needed for these pages.

RewriteEngine on
RewriteRule (.*) http://www.new.com/$1 [R=301,L]



BUT, there are a few pages whose names HAVE changed on the new domain.

Does anyone know if there's any way I can redirect specifically from:

old.com/oldname.html to new.com/somethingdifferent.html
old.com/oldname1.html to new.com/anotherdifferentname.html, etc

using the same .htaccess file on the old domain as I've already got the general rewrite in?
12:30 am on Apr 30, 2013 (gmt 0)

Junior Member

joined:Apr 6, 2013
posts:149
votes: 0


This is actually a run-of-the-mill redirect issue. Try checking the Apache docs for examples.

[httpd.apache.org...]
1:54 am on Apr 30, 2013 (gmt 0)

Administrator

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

joined:Aug 10, 2004
posts:10543
votes: 8


the more specific RewriteRule(s) should precede the more general RewriteRule and the target of the more specific RewriteRule(s) should include the canonical protocol and hostname to prevent multiple redirect hops.
2:13 am on Apr 30, 2013 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:12709
votes: 244


Does anyone know if there's any way I can redirect specifically from:

You betcha. But the exact method will depend on how many pages are involved. If it's just a few
--definitions of "few" vary of course, but I'd say less than 10 or 20--
write out a separate rule for each one. If there are a lot, rewrite (not redirect!) everything to a php script on the old site which will then look up the data and issue a redirect. If the changing URLs all have some feature in common, such as belonging to the same directory, you may be able to pull them aside in htaccess. If they're scattered all over the place, you'll probably have to start by rewriting everything to your php script --or RewriteMap, which is basically another way of achieving the same result.

Either way, any page-specific redirects or rewrites will have to come before a general, catch-all redirect. Do this even if it means putting a rewrite before a redirect (contrary to default practice).
7:03 am on Apr 30, 2013 (gmt 0)

New User

10+ Year Member

joined:Aug 17, 2004
posts:35
votes: 0


Thanks for your help. I've tried solutions based on the above, but I must be doing something wrong.

I'm trying to redirect
http://www.old.com/oldpagename.html
to
http://www.new.com/anothername.html
in addition to a general redirect for the many page names that don't change on the new domain.

I've tried:

RewriteEngine on
RewriteRule ^oldpagename\.html$ http://www.new.com/anothername.html [R=301]
RewriteRule (.*) http://www.new.com/$1 [R=301,L]



But this leads to
http://www.new.com/http:/www.new.com/anothername.html


I've tried:

RedirectPermanent /oldpagename.html http://www.new.com/anothername.html

RewriteEngine on
RewriteRule (.*) http://www.new.com/$1 [R=301,L]


But this leads to
http://www.new.com/oldpagename.html


I must be making a mistake, but I can't see what. Any suggestions?
12:01 pm on Apr 30, 2013 (gmt 0)

Junior Member

joined:Apr 6, 2013
posts:149
votes: 0


In your first attempt, I think you only need to add a "last" flag. Otherwise, Apache will continue to apply the other rewrite rules.

RewriteEngine on
RewriteRule ^oldpagename\.html$ http://www.new.com/anothername.html [R=301,L]
RewriteRule (.*) http://www.new.com/$1 [R=301,L]


And in your second attempt... admittedly this is not at all obvious... but when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file. So it would seem that you need to either use only Redirect directives or only RewriteRule directives to do redirections.
12:08 pm on Apr 30, 2013 (gmt 0)

Administrator

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

joined:Aug 10, 2004
posts:10543
votes: 8


Redirect and RewriteRule directives in the same scope


i would avoid mixing mod_alias and mod_rewrite directives anywhere within a virtual host configuration - in any scope/context.
3:39 pm on Apr 30, 2013 (gmt 0)

New User

10+ Year Member

joined:Aug 17, 2004
posts:35
votes: 0


Thanks Dideved, it's working perfectly now.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members