Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule exceptions.

         

MrBean

10:22 pm on Jan 1, 2009 (gmt 0)

10+ Year Member



Hello everyone!
First, I have to say that I've already looked in few subjects that contains many information about RewriteRule exceptions... But nothing worked :(

the problem is;

I wanna set a general rule for "virtual directories" on the server;
so I wrote this htacces file:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9]*)/([a-zA-Z0-9]*)/ $1.php?page=$2
RewriteRule ^([a-zA-Z0-9]*)/ $1.php

it works! the problem is that I wanna except the directory images and all the subdirectories behind (and the sub-subdirectories and the sub-sub-sub-directories etc...)

I tried to add
RewriteCond %{REQUEST_URI} !/images

But, it works only on the main directory "images" and not on all the subdirectories in it...

any ideas?

thanks ahead!

Oh, BTW sorry for my bad English
MrBean

jdMorgan

11:56 pm on Jan 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your concept is correct. Two things come to mind:
1) A RewriteCond applies only to the single RewriteRule that it precedes; Add the RewriteCond to both rules.
2) Always completely flush your browser cache before testing any new server-side code.

A note: Since it is unlikely that you want to rewrite URLs where the "[a-zA-Z0-9]" field is blank, I recommend modifying your pattern. In addition, you can make it more efficient by using both the [NC] (No Case) and [L] (last rule) flags, and anchoring your patterns:


RewriteEngine on
#
RewriteCond $1 !^images
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/$ $1.php?page=$2 [NC,L]
#
RewriteCond $1 !^images
RewriteRule ^([a-z0-9]+)/$ $1.php [NC,L]

Jim

MrBean

12:05 am on Jan 2, 2009 (gmt 0)

10+ Year Member



Jim,

First of all - BIG BIG BIG thank you!
you solved the problem. :)

tell me again (in simple words please) what's wrong with ([a-zA-Z0-9]*) and what does the (No Case) and [L] (last rule) means exactly?

Thanks again

jdMorgan

1:14 am on Jan 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should look up those flags in the Apache mod_rewrite documentation [httpd.apache.org], rather than asking members to reiterate that information here. Reading that documentation is a prerequisite to successful use of mod_rewrite, and frankly, I consider mod_rewrite to be quite dangerous to use without first reading the 'instructions' -- One typo, one misunderstood or mis-applied regular-expressions pattern, and you can sink your site in the search listings.

Answering indirectly to perhaps pique your interest:

Using [NC] speeds up that letters-and-numbers check by over 33%.

Using [L] tells mod_rewrite not to bother proceeding further into your rules if the current rule matches, again speeding up your server.

Links to other useful documentation are available in our Apache Forum Charter [webmasterworld.com].

Jim

g1smd

1:21 am on Jan 2, 2009 (gmt 0)

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



Using the
*
in
([a-zA-Z0-9]*)
would allow the value to be BLANK. Look at what
*
means.

Using

+
in there means there must be at least one character present. Check what
+
means.

Always add

[L]
unless there is a very very good reason to omit it. There usually isn't a good reason.