Forum Moderators: phranque

Message Too Old, No Replies

301 redirect rules causing a double jump

good/bad...how do you fix it?

         

jlander

1:35 am on Apr 11, 2010 (gmt 0)

10+ Year Member



I was doing a little reading and found a reference that some 301 redirect rules can cause a double jump. I've tried to find more information on this and have been unsuccessful.

I really get confused when I open up my .htaccess file; but, I am moving a site and decided to tackle this job myself. My problem is that under some conditions (when dynamic content is present), I'm getting that double jump.

Here is a snippet of my .htaccess
    [size=2]RewriteEngine On

    rewriteCond %{HTTP_HOST} ^aaa\.bbb\.ccc\.ddd
    rewriteRule (.*) http://www.domain.com/$1 [R=301]

    RewriteCond %{HTTP_HOST} ^domain.com [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]

    RewriteCond %{QUERY_STRING} ^prod=12345$ [NC]
    RewriteRule ^proddetail\.asp$ http://www.domain.com/index.php?_a=viewProd&productId=2 [NC,R=301,L][/size]


Can someone tell me if this is a problem and how to fix it. Should I simply remove the [R=301] from the last rule shown?

jlander

2:01 am on Apr 11, 2010 (gmt 0)

10+ Year Member



Should I simply remove the [R=301] from the last rule shown?


No. That won't work. Then the bulk of my product page redirects won't work properly.

g1smd

6:34 am on Apr 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Rule ordering is important, put the most specific rule first, i.e the one affecting only one URL. Make sure that rule redirects to correct domain name at the same time.

jlander

12:50 pm on Apr 11, 2010 (gmt 0)

10+ Year Member



That makes sense. I have a lot of other rewrites under those that rewrite the category and product URLs to be more readable. Should I put the top 2 redirects at the very bottom of the .htaccess file to let those other rewrites happen firs, or should I put them under the last 301 rewrite?

g1smd

1:27 pm on Apr 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Be careful with your use of "rewrite" and "redirect". Those two words mean quite different things.

List all external redirects first, from most specific to most general. The last rule is usually the canonical non-www to www redirect.

List the internal rewrites after that, again from most specific to most general.

Redirects tell the user to go fetch a different URL. Rewrites fetch the content from inside the server; it's too late to be redirecting by then.

Make sure all rules use RewriteRule directives. Don't use Redirect or RedirectMatch in the same file.

jdMorgan

1:41 pm on Apr 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Be very careful here. Be sure you know the very big difference between an external client redirect (a URL-to-URL redirect) and an internal URL-to-filepath-translation rewrite, and take care to use the proper term, both when posting and when thinking about your code. Your chances of 100% correct operation are much better by doing that.

Put all of your external redirects first, in order from most-specific patterns and conditions (fewest URLs affected) to least specific patterns and conditions (more URLs affected), followed by all of your internal rewrites, again in order from most- to least-specific.

This prevents unexpected matches and rule invocations, stacked/chained redirects, and the exposure to Web clients of your internally-rewritten filepaths as URLs by a redirect invoked after a rewrite.

Note that this redirect-rewrite ordering guideline applies across *all* config and .htaccess files - For any given HTTP request, no rewrite must be allowed to execute until all redirects have been invoked.

[added] g1smd beat me to it -- I had this thread open too long in my browser... :) [/added]

Jim

jlander

3:36 pm on Apr 11, 2010 (gmt 0)

10+ Year Member



g1smd and jMorgan,

Thanks for your help. I understand what you are saying, but am still a little foggy about its implementation. Thanks to your help, I've eliminated the double jump that was caused by the canonical redirect being put before my product redirects. I've moved those, but if I understand you correctly, I probably have one other block of rewrites to move.

In my .htaccess file I now have:

1. 301 redirects for my old product pages to my new ones.
2. Canonical redirect for non-WWW to WWW & IP to domain.
3. Rewrites for the categories and articles.
4. Rewrites for specific xml files.

I should probably do the specific xml rewrites before the more general category and article rewrites. Is that true?

jdMorgan

4:06 pm on Apr 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If none of the categories and articles match the .xml URL-path requests, then it doesn't matter which go first, except that you might want to order them most-frequently-requested first for the sake of efficiency.

The order within each of the two redirect and rewrite rule groups only matters if the patterns of the rules can all match the same request, as you experienced with your initial attempt.

In the case at hand, a rule pattern for a category or an article URL-path may or may not match a request for a .xml document. But the rule pattern for a specific .xml document URL-path is very unlikely to match a category or article rule pattern.

As you can see, it's all about RewriteRule patterns matching requested URL-paths. If you think in terms of pattern-matching, this all makes a lot more sense.

Jim