Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply rewrite rule if url not contains certain path

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!

like image 944
JBerta93 Avatar asked Aug 17 '14 18:08

JBerta93


People also ask

Does the URL in the browser change when a rewrite happens?

A rewrite is a server-side operation that occurs before a web server has fully processed a request. A rewrite is not visible to website visitors, since the URL displayed in the browser does not change.

What is $1 in Apache RewriteRule?

$1 represents the match from the first set of parentheses in the RewriteRule regex, not in the RewriteCond regex.

What does RewriteRule do?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.


1 Answers

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]
like image 110
Justin Iurman Avatar answered Nov 01 '22 15:11

Justin Iurman