views:

192

answers:

3

Hi,

i'm trying to rewrite my urls to goto a single php file:

RewriteRule ^dir/(?:(?!style|js).)*$ http://www.domain.com/single.php?uri=$1 [QSA]

However the exclusion of /dir/js and /dir/style isn't working as i was hoping it would...

  • [redirects] domain.com/dir
  • [redirects] domain.com/dir/jason
  • [redirects] domain.com/dir/jason/pete
  • [DOESN'T REDIRECT: GOOD] domain.com/dir/js
  • [DOESN'T REDIRECT: GOOD] domain.com/dir/js/*
  • [DOESN'T REDIRECT: BAD] domain.com/dir/json

How can I change the regular expression to match my needs?

A: 

EDITED:

domain.com/dir/json doesn't redirect because it doesn't match the regex.

The reason /dir/json doesn't redirect is because js follows dir/, and your regex only matches when dir/ is not followed by either style or js. I think negative lookaheads are the wrong approach. I think what you actually want is something like:

RewriteCond %{REQUEST_URI} !^/dir/(js|style)(/.*)?$
RewriteRule ^dir/(.*)$ http://www.domain.com/single.php?uri=$1 [LQSA]

That basically means if the URL isn't ended with /js or /style (optionally with further path components underneath those dirs), then apply the redirect rule.

Matthew Flaschen
+2  A: 

Try to replace style|js with style\b|js\b.

Maybe RewriteCond could be of use like in

RewriteCond %{REQUEST_URI} !^/dir/(style|js)($|/)
RewriteRule ^/dir/(.*)$ http://www.domain.com/single.php?uri=$1 [QSA]
Gleb
Thanks for helping me fix my broken syntax. The / at the end of yours should be optional. Otherwise domain.com/js will redirect.
Matthew Flaschen
Edited for optional '/'.
Gleb
A: 

Either with your negative look-ahead assertion:

RewriteRule ^dir/(?!(?:style|js)(?:/|$))(.*) http://www.example.com/single.php?uri=$1 [QSA]

But that’s not so nice.

Or you just add another test on how $1 starts:

RewriteCond $1 !^(style|js)(/|$)
RewriteRule ^dir/(.*) http://www.example.com/single.php?uri=$1 [QSA]
Gumbo