Welcome to WebmasterWorld Guest from 54.221.49.52

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect Conflict

     

rwilson

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/

phranque

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

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



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

rwilson

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]

phranque

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

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



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.

lucy24

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

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



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

rwilson

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



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

phranque

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

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



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
 

Featured Threads

Hot Threads This Week

Hot Threads This Month