homepage Welcome to WebmasterWorld Guest from
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 Conflict

 8:22 pm on Sep 13, 2012 (gmt 0)

How can I get both of these rules to work?
RewriteRule ^example/? /new-example/ [R=302,L]
RewriteRule ^example/new-stuff/? /new-example/new-stuff/ [R=302,L]

example.com/example/new-stuff/ returns example.com/example/



 11:06 pm on Sep 13, 2012 (gmt 0)

you must order your directives from the most specific rule to the most general rule.

(are you sure that should be a 302 vs 301?)


 12:13 am on Sep 14, 2012 (gmt 0)

I meant to have 301, I was just using 302 for testing. So if I just switch the order around and say the most specific first I should be good?

RewriteRule ^example/new-stuff/? /new-example/new-stuff/ [R=302,L]
RewriteRule ^example/? /new-example/ [R=302,L]


 1:43 am on Sep 14, 2012 (gmt 0)

assuming "example" is the same in both rules, you can actually do that in one rule by capturing the optional subdirectory in the pattern and using the back reference in the target.


 2:27 am on Sep 14, 2012 (gmt 0)

... and as long as you're there, may as well allow for "index.html" -- or php or whatever it is you really use -- so you can redirect those in the same rule:

directory(/(index\.html)?)?$ {et cetera}

Otherwise some people will be redirected twice.


 2:28 am on Sep 14, 2012 (gmt 0)

Cool! "example" is the same, how would I go about doing that?


 4:55 am on Sep 14, 2012 (gmt 0)

you can group something in your pattern by surrounding it with parentheses.
when something is captured in a group it can be reused by referring back to it with a variable in the substitution.

Back-references are identifiers of the form $N (N=0..9), which will be replaced by the contents of the Nth group of the matched Pattern.:

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