Multiple 301 redirects

Mar 25, 2010 at 11:38 AM

Have an iirf.ini file with several rules to convert an origin Url into a 'nice' destination Url. These are my rules:

RedirectRule ^(.*)$  #L$1#E     [R=301]
RedirectRule ^/(.*)$        http://www.domain.com/$1  [R=301]
RedirectRule ^(.*)/default.aspx(.*)$  $1/$2  [R=301]

My problem is that instead of IIRF applying all the rules and generate a single 301 redirect, it is applying one rule generating a 301 redirect, applying the next rule generating a 301 and applying the 3rd rule and another redirect. This is quite ugly. Is there any way to only have a 301 redirect after the evaluation of all the rules? Am I doing something wrong?

Thanks

Riga

Coordinator
Mar 25, 2010 at 3:10 PM
Edited Mar 25, 2010 at 3:13 PM

Yes, you're doing something wrong.

All of your rules are, in fact, redirections.  From your description, IIRF appears to be doing what you instructed it to do.  As described in the documentation, when there's a RedirectRule that "fires" , the redirect is returned to the client browser immediately, with no further processing of any additional IIRF rules.   

So IIRF is working correctly, according to your rules, and your description of the behavior. The problem is, I don't know what you really want to do. Sometimes I can guess, from the way the rules are written, at what people really want, as opposed to what the rules do.  But in your case I'm not sure. Let me try.

I think you want to downcase incoming requests.  Ok.  That sounds reasonable, but you do so, ALWAYS.   Is that right?  Probably not.  You probably want a condition in there to determine if downcasing is appropriate.  Downcasing is only needed if there are uppercase chars in the URL, I would suppose, eh?  ok, so that's  the first rule.

For the 2nd rule, you also want to redirect to www.domain.com, but.... always?  Again you want a condition, but which one? I'm going to guess that its whenever the URL does not begin with www.

For the 3rd rule, you want to transform the query path.   Here, though, I'm not sure what you want to do.   I think it's... if there are any clients out there using URLs that include default.aspx, then redirect them to the cleaner URL syntax, is that right?

If all that is true then I suggest you rework things a bit. Add a condition to each rule. Also - very important - reorder the rules to change the order of evaluation.  Always put the most specific rules on top.  Because IIRF evaluates from top to bottom, it will filter out the specific cases before falling into the more general ones.

Something like this:

# redirect any request for a domain name that is not www, with a URL 
# that includes default.aspx, into a request for www.domain.com .  
# Also downcase, while redirecting. 
RewriteCond %{HTTP_HOST}    ^(?!www)([^.]+)\.mydomain\.com [I]
RedirectRule ^/(.*)/default.aspx(.*)$    http://www.mydomain.com/#L$1/$2#E  [R=301]

# redirect any request for a domain name that is not www, into
# a request for www.domain.com .  Also downcase, while redirecting. 
RewriteCond %{HTTP_HOST}    ^(?!www)([^.]+)\.mydomain\.com [I]
RedirectRule ^/(.*)$        http://www.mydomain.com/#L$1#E  [R=301]

# Redirect any URL with this shape, into a simpler form. 
# Also, downcase, just in case.
RedirectRule ^/(.*)/default.aspx(.*)$  #L$1/$2#E  [R=301]

# Finally, downcase as necessary.  If there are any
# uppercase characters in the URL, apply this rule. 
RewriteCond %{url}   [A-Z]+ 
RedirectRule ^(.*)$  #L$1#E     [R=301]

 

Mar 25, 2010 at 4:50 PM

Many thanks for your feedback.

You are right about my rules in all the cases, this is what I want to do. I have RewriteCond for all the RedirectRule but did not add them here to make shorter the post and clear that my problem was the multiple 301 redirects.

I read the documentation but I missed this part about rule processing and redirects. Suppose there is some reason for that but will be nice if only one a single redirect is executed. Even if I reorder the rules there is always the possibility that multiple redirects are executed.

Have to think what to do now.

Thanks again for your help.