Apply rewrite rule if url not contains certain path

13,437

Your third RewriteCond is useless since you're already checking for existing files in first condition.

Also, if you're in a subdirectory (like /old/ or something, you should add a RewriteBase or a leading slash to your index.html. Otherwise it will look in current directory (404 error probably).

You can replace your code by this one

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(old|images)/ [NC]
RewriteRule . /index.html  [L]
Share:
13,437
JBerta93
Author by

JBerta93

I'm a young developer, i know Java SE and i use Java EE to develop a web application, however i know PHP(my first programming language)!I know also Posgresql and Mysql. I had some experience with C/C++ and ASM but my knowledge of this language are basic! I had develop some project with Arduino(using Wiring and C++) and Processing! For my last project i'm using HTML5 and JQuery!

Updated on June 16, 2022

Comments

  • JBerta93
    JBerta93 almost 2 years

    i've written this rewrite rule for my AngularJs application that use html5 routing.

    The rewrite url is this:

     RewriteEngine On
     RewriteCond %{REQUEST_FILENAME} !-f
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png|html|woff)$
     RewriteRule ^ index.html  [L]
    

    I want that all the URL that contains path /old/ and with path /images/ aren't rewritten!

    Someone can help me i tried to add rewrite condition like this :

     RewriteCond %{REQUEST_URI} !^/old/
    

    But it doesn't work!

    Regards, Lorenzo

    UPDATE

    If you're using AngularJS with routing use target="_self" on <a> element that has link in the same domain but aren't under the angular routing!