Forum Moderators: phranque

Message Too Old, No Replies

Help with regular expression syntax

Want "and not" logic with RewriteRule

         

Merganser

4:28 pm on Mar 10, 2012 (gmt 0)

10+ Year Member



I am trying to structure a Rewrite Rule that would apply to all .php pages with exception of 2 specific .php pages (lets call them abc.php and def.php). I am having trouble figuring out the syntax to accomplish this. Specifically, my difficulty resides in generating the "and not" boolean logic. I have been experimenting by combining the following 3 structures but have not been able to get the proper functionality as of yet. Any help would be appreciated.

\.php
!(abc\.php)
!(def\.php)

My other difficulty is testing. How can you cause the RewriteRule to output a test string (or otherwise somehow indicate) if the conditions are met?

Thanks for your help.

lucy24

9:03 pm on Mar 10, 2012 (gmt 0)

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



Second question first: This is easy. I do it regularly. You redirect from a nonexistent page to a different nonexistent page, like this:

RewriteCond {blahblah}
RewriteCond {blahblah}
RewriteRule foobar otherfoobar.html [R=301,L]

The request has to be for a nonexistent page so you don't accidentally pick up unsuspecting human visitors. The target has to be a redirect so that your browser's address bar changes. If the rule executes, you will land on your 404 page but the address bar will say www.example.com/otherfoobar.html

Now, the first rule: By default, Rewrite Conditions are "and" rather than "or". So you don't need to say "and". The basic pattern is simply

RewriteCond %{REQUEST_URI} !(excludethispage|andthispage)
RewriteRule \.php {do stuff here}

If there are multiple pages with overlapping names, you may need anchors or further constraints in the Condition. I've shown the simplest form: ! meaning "not" and then the two pipe-separated options. The parentheses are crucial, even though you're not capturing, because otherwise it would mean "is not abc OR is def" instead of the desired "is NEITHER abc NOR def".

g1smd

9:20 pm on Mar 10, 2012 (gmt 0)

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



The basic form using a pipe symbol for OR is as follows:

RewriteCond %{REQUEST_URI} !^/(this|that)\.php$
RewriteRule \.php$ /target [L]


Be aware that "NOT (this OR that)" is functionally equivalent to "(NOT this) AND (NOT that)".

Merganser

5:33 pm on Mar 11, 2012 (gmt 0)

10+ Year Member



Ok - Thanks guys, this is what I needed.

This is basically how I was performing the testing but I was hoping for a more convenient method.

I guess I had a brain lapse on the "and not" logic. I am so used to using [OR] as part of the RewriteCond that I have come to instinctively associate multiple RewriteConds as being OR'd and lost sight of the default AND functionality. Thus, I was trying to accomplish it within the RewriteRule instead of with use of a RewriteCond.