tags:

views:

170

answers:

2

I've got a small problem. I've got a good setup which mod rewrites all requests to the site - the only thing is it also rewrites directories which I don't want to be included.

I'm using this code in my .htaccess file:

RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?section=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&page=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&page=$2&split=$3 [L]

Ideally I'd like to be able to exclude two directories - access/ and edit/ - edit/ also needs to have it's own set of rules:

RewriteRule ^([^/\.]+)/?$ index.php?action=$1 [L]

I can get around this problem by linking directly to the .php file in either directory, but this isn't ideal.

Any advice?

+2  A: 

Use RewriteCond

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(access|edit)/
RewriteRule ^([^/\.]+)/?$ index.php?section=$1 [L]
...

(This is untested, but it should be close)

Sean Bright
Yeah, it should be close. And then add a RewriteCond for the edit subdir too.
PEZ
No dice. I think the server ignores the condition and goes straight to the first rewrite rule. I think it must be to do with how the rewrite rule is written, but my regex is very limited - even doing this was trying to find an appropriate tutorial.
different
FWIW I think it should be %{REQUEST_URI} instead of ${REQUEST_URI}
David Zaslavsky
David, you're absolutely right. I've updated the code.
Sean Bright
+1  A: 

An alternate idea (also untested):

RewriteEngine on
RewriteRule ^/access/ - [L]
RewriteRule ^/edit/([^/\.]+)/?$ /edit/index.php?action=$1 [L]
... (other rules)

which would save you from having to repeat the RewriteCond before every rule.

David Zaslavsky
Yes, that is definitely more elegant.
Sean Bright
The second line works. But the server is still using the other rules for the edit/ directory.
different
Maybe try a less restrictive regular expression? like ^/edit/(.*)$
David Zaslavsky
Still no joy... unfortunately.
different