How to rewrite ALL urls when skipping certain folders?
I am trying to rewrite my urls to navigate to a single php file:
RewriteRule ^dir/(?:(?!style|js).)*$ http://www.domain.com/single.php?uri=$1 [QSA]
However, excluding / dir / js and / dir / style does not work as I was hoping it was ...
- [redirects] domain.com/dir
- [redirects] domain.com/dir/jason
- [redirects] domain.com/dir/jason/pete
- [DOES NOT PASS: GOOD] domain.com/dir/js
- [DOES NOT TRANSFER: GOOD] domain.com/dir/js/*
- [DOES NOT TRANSFER: BAD] domain.com/dir/ js on
How can I change the regex to suit my needs?
a source to share
Editorial staff:
domain.com/dir/json is not redirected because it doesn't match regex.
The reason / dir / json is not being redirected is because js follows dir / and your regex only matches when dir / follows neither style nor js. I think negative views are the wrong approach. I think you really want something like:
RewriteCond %{REQUEST_URI} !^/dir/(js|style)(/.*)?$
RewriteRule ^dir/(.*)$ http://www.domain.com/single.php?uri=$1 [LQSA]
This basically means the url doesn't end with / js or / style (optionally with additional path components below those dirs), then apply the redirect rule.
a source to share
Or with your own negative expectation:
RewriteRule ^dir/(?!(?:style|js)(?:/|$))(.*) http://www.example.com/single.php?uri=$1 [QSA]
But this is not so pleasant.
Or you just add another test about startup $1
:
RewriteCond $1 !^(style|js)(/|$)
RewriteRule ^dir/(.*) http://www.example.com/single.php?uri=$1 [QSA]
a source to share