Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess RewriteRule also rewriting my css, js and images files. how to ignore these?

I want that when user input http://example.com/abc/123/jon/ in URL is actually show this file http://example.com/abc.php?id=123&name=jon and this is easily can be done by adding these lines into htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^abc/(.*)/(.*)/ abc.php?id=$1&name=$2 [L]

but my abc.php file having some (css, js) files:

<link href="css/style.css" rel="stylesheet">
<script src="js/jquery-1.10.2.js"></script>

and images like : img/logo.png

but because of my .htaccess file it is searching for style.css here : http://example.com/abc/123/jon/css/style.css instead of http://example.com/css/style.css and similarly for js and images.

i already tried many similar answers here but none of them working for me.i am looking for .htaccess solutions for this.

like image 664
shubh jaiswal Avatar asked Jan 13 '15 13:01

shubh jaiswal


2 Answers

You can use that:

RewriteEngine on
# not rewrite css, js and images
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^abc/(.+)/(.+)/ abc.php?id=$1&name=$2 [L]

# rewrite css, js and images, from root
RewriteRule ^abc/[^/]+/[^/]+/(.+)$ $1  [L]

If your final css (or js) directory is not at root, you can add the path before the last $1. Now it's: /css/style.css with /abc/xxx/yyy/css/style.css

like image 62
Croises Avatar answered Oct 15 '22 15:10

Croises


Just add before the RewriteRule one line:

RewriteCond %{REQUEST_FILENAME} !-f

This means that if your request uri points at existing file, then next RewriteRule will not trigger.

If you want other way then this one should do:

RewriteCond %{REQUEST_FILENAME} !\.(css|js|png|jpg)$

You can add more extensions separated by pole (|)

like image 6
Forien Avatar answered Oct 15 '22 14:10

Forien