RewriteCond Help

Topics: Developer Forum
Jun 18, 2008 at 7:51 PM
I'm going in circles here, I checked the readme file, have been searching google for 2 days to see what's wrong with my RewriteCond and I just can't figure it out (i'm a n00b at this as you might have guessed). Here is what I have:

RewriteCond %{SCRIPT_NAME} !^/read.php [I,OR]
RewriteCond %{SCRIPT_NAME} !.*\.html$ [I]
RewriteRule ^(.*)$ /index.php [L]

What i'm trying to do is, if the SCRIPT_NAME does not begin with /read.php OR end with .html, redirect to /index.php. So basically if I try to access www.domain.com/foo.php, it should redirect me to /index.php, but if I access www.domain.com/read.php, I do not get redirected.

What happens now is that if I access www.domain.com/foo.php or anything else that is not read.php, I do not get redirected either.

Frankly I have no idea if i'm doing this the right way or not, any help would be appreciated with this.
Coordinator
Jun 18, 2008 at 10:49 PM
I will look at this a little bit later.
Coordinator
Jun 19, 2008 at 6:06 AM

You have your logic a little twisted. "If the script name does not begin with read.php OR end with .html" isn't quite right. You want... "if (the script name is not read.php) AND (the script is not an .html page)" AND, not OR.

Also the regex needed a little tweak.

I think what you might want is something close to this:

RewriteCond %{SCRIPT_NAME} ^/(?!read.php).+$ [I]
RewriteCond %{SCRIPT_NAME} ^/(?!.*\.html).+$ [I]
RewriteRule ^(.*)$ /index.php [L]

This says, if the SCRIPT_NAME is something [.*], but not "read.php" [(?!read.php)] , ....AND.... if the script name is NOT an html page, then ... Rewrite it to index.php. (using case insensitive string matches in both conditions).  Remember, the AND is implicit.

Jun 19, 2008 at 2:12 PM
Ahhh now it makes a little more sense. I was going blind on this one, couldn't find any examples that did what I needed, so I had to improvise with my limited knowledge.Works like a charm, thanks alot for your help Cheeso!
Coordinator
Jun 19, 2008 at 5:44 PM
Super!  Glad that worked out for ya.
ps: don't forget, IIRF is now donationware.
I am now accepting donations on behalf of my favorite charity.
If you find IIRF useful, consider donating.