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

Apache Web Server Forum

    
A tricky htaccess redirect issue
can't work this one out
Doug10




msg:4569068
 11:30 pm on Apr 29, 2013 (gmt 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?

 

Dideved




msg:4569092
 12:30 am on Apr 30, 2013 (gmt 0)

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

https://httpd.apache.org/docs/2.4/rewrite/remapping.html

phranque




msg:4569133
 1:54 am on Apr 30, 2013 (gmt 0)

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.

lucy24




msg:4569142
 2:13 am on Apr 30, 2013 (gmt 0)

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).

Doug10




msg:4569237
 7:03 am on Apr 30, 2013 (gmt 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?

Dideved




msg:4569303
 12:01 pm on Apr 30, 2013 (gmt 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.

phranque




msg:4569304
 12:08 pm on Apr 30, 2013 (gmt 0)

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.

Doug10




msg:4569362
 3:39 pm on Apr 30, 2013 (gmt 0)

Thanks Dideved, it's working perfectly now.

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