Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Rule, How to Stop if a Match is Found

         

HoboTraveler

6:28 pm on Dec 19, 2008 (gmt 0)

10+ Year Member



Hi All,

Is there a parameter that can be specified in a rewrite rule that says to stop if a match is found?

Example: Stop at this rule if the URL matches this pattern.

TIA

jdMorgan

7:10 pm on Dec 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is the function of the [L] flag.

However, you need to understand that mod_rewrite processing in .htaccess is recursive; If any rewrite or redirect is invoked, then the mod_rewrite rule processing is re-started from the top, in order to check for further rules that apply to the new URL-path -- for example for access control/security.

So in order to prevent recursion problems, you need to explicitly prevent some rules from being applied more than once within the context of the current HTTP request. This can be done based on the URL (which will have been updated), the query string (if it has been changed), or on other factors 'visible' to RewriteConds or RewriteRules.

As a last-ditch fix, you can create a user-variable using [E=var-name:var-value] and testing that variable using "RewriteCond %{ENV:var-name} !^var-value$" in order to prevent the following rule from being re-invoked.

Jim

HoboTraveler

4:43 am on Dec 20, 2008 (gmt 0)

10+ Year Member



Could you please post an example on how I could prevent recursion problems?

How do I prevent some rules from being applied more than once?

Thanks.

jdMorgan

2:30 pm on Dec 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are hundreds of examples already posted in this forum. Here is one of the most common:

RewriteCond $1 !^index\.php$
RewriteRule ^(.*)$ /index.php?page=$1 [L]

The most efficient solution depends on the problem; If you have a specific question about a specific problem, please post it.

Jim