Forum Moderators: phranque

Message Too Old, No Replies

.htaccess running twice?

looks like .htaccess runs twice? First rewriterule executed after the 2nd

         

RewriteEngine

6:37 pm on Sep 3, 2007 (gmt 0)

10+ Year Member



It looks like my .htaccess runs twice - it's looping!? Here is what happends:

mydomain.com/catalog/test.php?something
=(301)=> mydomain.com/en/test.php
=(301)=> mydomain.com/en/test.php
=(301)=> ...

My .htaccess file:

Options -Indexes
Options FollowSymLinks

RewriteEngine on
RewriteBase /

RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^catalog/test.php$ en/test.php?[R=301,L]

RewriteRule ^(frŠenŠes)/(.*)$ /catalog/$2?language=$1 [QSA,L]

Looks like the first rewriterule is executed after the second rewrite rule. I have tried removing both "lastrule" [L] but that didn't help. How can I make my .htaccess only run once?

g1smd

10:35 pm on Sep 3, 2007 (gmt 0)

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



You will either need to reverse the two rules, or you will need to add a RewriteCond to the second rule. With rules like this, normally the most specific should be first and the more general last.

Without knowing what you are trying to redirect from and too with each of those rules, and the cases for when each should run, or not run, I can't offer any sample code.

jdMorgan

11:38 pm on Sep 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To stop the loop, check the request header received from the client:

# Externally redirect from /catalog/test.php to /en/test.php, removing
# the query string, but only if the request is a direct client request
# and not a result of the following internal rewrite
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /catalog/test\.php\?
RewriteRule ^catalog/test\.php$ http://www.example.com/en/test.php? [R=301,L]
#
# Internally rewrite /<language>/<anything>
# to catalog/<anything>?language=<language>,
# retaining any pre-existing query string
RewriteRule ^(frŠenŠes)/(.*)$ /catalog/$2?language=$1 [QSA,L]

With the added RewriteCond, the external redirect will only be invoked if a client (browser or robot) asks for /catalog/test.php directly, and not if the requested URL has been internally rewritten to /catalog/test.php by the second rule (an internal rewrite cannot change the original request header received from the client).

I removed your QUERY_STRING RewriteCond, since it didn't do anything other than burn CPU cycles, and I couldn't discern what it might have been intended to do.

Replace the broken pipe "Š" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

RewriteEngine

6:12 pm on Sep 4, 2007 (gmt 0)

10+ Year Member



>> RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /catalog/test\.php\?

Thank you very much. That does the trick! However, I can't figure what this means: [A-Z]{3,9}\

Is the {3,9) a position thing for letters A-Z, or? And why is it escaped?

g1smd

10:50 pm on Sep 4, 2007 (gmt 0)

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



[A-Z] is anything "A to Z"

{3,9} means "between 3 and 9 characters" (GET, POST, etc)

\ means "followed by a literal space".

You might need to add an [NC] to the end of the line to cater for "Any Case".

jdMorgan

12:02 am on Sep 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



THE_REQUEST is the entire client request header, which in this case, might look like:
GET /catalog/test.php?something HTTP/1.1

So the [A-Z]{3,9} subpattern matches the "GET" at the beginning, or any of the other HTTP methods which, if valid, range in length from 3 to 9 characters.

Jim