Welcome to WebmasterWorld Guest from 107.20.54.98

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect Conflict

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

Junior Member

5+ Year Member

joined:Aug 17, 2010
posts: 186
votes: 2


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 Sept 13, 2012 (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:10544
votes: 8


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 Sept 14, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 17, 2010
posts: 186
votes: 2


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 Sept 14, 2012 (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:10544
votes: 8


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 Sept 14, 2012 (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:12719
votes: 244


... 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 Sept 14, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Aug 17, 2010
posts: 186
votes: 2


Cool! "example" is the same, how would I go about doing that?
4:55 am on Sept 14, 2012 (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:10544
votes: 8


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.:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule