3

So I have the following rewrite rules:

RewriteRule ([^/]*)/([^/]*)/([^/]*)$ /index.php?grandparent=$1&parent=$2&page=$3
RewriteRule ([^/]*)/([^/]*)$ /index.php?&parent=$1&page=$2
RewriteRule ([^/]*)$ /index.php?page=$1    

But I need this to not pass to the index page for some sub-domains, so I have the following rewrite conditions before this:

RewriteCond %{REQUEST_URI} !^/css/.*$
RewriteCond %{REQUEST_URI} !^/js/.*$
RewriteCond %{REQUEST_URI} !^/admin/.*$

So that the rules will not apply when looking for any file in those directories (including files that may be in sub-directories of those directories). Yet they still keep getting rewritten to index.php.

How can I make exceptions for these directories?

anubhava
  • 761,203
  • 64
  • 569
  • 643
GSto
  • 41,512
  • 37
  • 133
  • 184

2 Answers2

7

Your RewriteCond should be like this:

RewriteCond %{REQUEST_URI} ^/(admin|js|css)/ [NC]

Additionally you must use L and QSA flags in all the RewriteRule lines like this:

# skip these paths for redirection
RewriteCond %{REQUEST_URI} ^/(admin|js|css)/ [NC]
RewriteRule ^ - [L]

RewriteRule ([^/]+)/([^/]+)/([^/]+)/?$ index.php?grandparent=$1&parent=$2&page=$3 [L,QSA]
RewriteRule ([^/]+)/([^/]+)/?$ index.php?&parent=$1&page=$2 [L,QSA]
RewriteRule ([^/]+)/?$ index.php?page=$1 [L,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 1
    This worked for me but surprisingly without exclamation mark like `RewriteCond %{REQUEST_URI} ^/(admin|js|css)/ [NC]`. I'm not an htaccess mod-rewrite expert but maybe it should be edited? – cyborg86pl Mar 05 '14 at 16:48
4

Try this:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^/(admin|css|js)/
RewriteRule . - [S=3] #skip the next 3 rules if the RewriteCond match

RewriteRule ([^/]*)/?$ index.php?page=$1 [NC,L,QSA]
RewriteRule ([^/]*)/([^/]*)/?$ index.php?&parent=$1&page=$2 [NC,L,QSA]
RewriteRule ([^/]*)/([^/]*)/([^/]*)/?$ index.php?grandparent=$1&parent=$2&page=$3 [NC,L,QSA]
EscoMaji
  • 763
  • 5
  • 14