Welcome to WebmasterWorld Guest from 50.19.156.133

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

avoiding a double 301 response issue

is there a way to prioritize rules in htaccess?

     

icarus

10:35 pm on May 13, 2006 (gmt 0)

10+ Year Member



Greetings,

I've had a few 301 redirects in place for a number of years now, but late last year I also needed to introduce a rewrite to get around canonical problems with G. The relevant contents of .htaccess are:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.com [NC]
RewriteRule ^(.*)$ [site.com...] [L,R=301]

and:

redirect 301 /folder/file.htm [site.com...]

.. so what's happening now is that if there's a request for

[site.com...]

I wind up with 2 301 responses before the correct page is fetched, first the rewrite to www and then the redirect to the correct page.

I've tried putting the

redirect 301

line first in .htaccess, but the same issue occurs. Is there a way around this double 301 effect?

Thanks in advance for your assistance!

jdMorgan

11:48 pm on May 13, 2006 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You are using directives from two different Apache modules, mod_alias, and mod_rewrite.

Each of those modules will parse your .htaccess file in turn, executing the directives that it understands, with the order determined by the server configuration. Therefore, it makes no difference what order yo put your two directives -- re-arranging them does not change their execution order.

This applies to all other directives in config files -- You cannot control order of execution on a per-directive basis unless those directives are all implemented in the same module. The module execution order is set by the reverse LoadModule order in Apache 1.x, and by an internal priority scheme in Apache 2.x

The solution is to use either mod_alias or mod_rewrite, but not both, for sequence-critical rewrites and redirects. Since mod_alias cannot do conditional redirects and cannot do internal rewrites, I'd suggest using mod_rewrite for your folder redirect.

Jim

icarus

1:40 am on May 14, 2006 (gmt 0)

10+ Year Member



Hi Jim,

As always, thank you :)

I've just tried (after removing the mod alias rule)

Options +FollowSymLinks
RewriteEngine on
rewriteRule ^/folder/file\.htm$ [site.com...] [R=301,L]
RewriteCond %{HTTP_HOST} ^site\.com [NC]
RewriteRule ^(.*)$ [site.com...] [L,R=301]

and also as a separate entry:

rewriteEngine on
rewriteRule ^/folder/file\.htm$ [site.com...] [R=301,L]

(tried both above and below the http:// to [www...] rule)

.. but I just wind up with a 404 as the http:// to [www...] rule kicks in first, and the other rule is ignored - could you please advise as to where I'm going wrong?

jdMorgan

1:22 pm on May 14, 2006 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



In a per-directory (.htaccess) context, the URLs 'seen' by RewriteRule are localized to the directory in which the .htaccess file containing the executing RewriteRule is located -- The path to the local directory is stripped. In other words, URLs 'seen' by a RewriteRule running in your home directory .htaccess won't contain a leading slash. :)

Options +FollowSymLinks
RewriteEngine on
#
RewriteRu[b]le ^fo[/b]lder/file\.htm$ http://www.example.com/folder/file2.htm [R=301,L]
#
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Jim

icarus

8:16 pm on May 14, 2006 (gmt 0)

10+ Year Member



Thanks Jim! All working now, no more double 301 response :)
 

Featured Threads

Hot Threads This Week

Hot Threads This Month