homepage Welcome to WebmasterWorld Guest from 23.20.91.134
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
avoiding a double 301 response issue
is there a way to prioritize rules in htaccess?
icarus




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

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




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

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




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

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




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

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




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved