shubh jaiswal
shubh jaiswal

Reputation: 438

.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.

Upvotes: 8

Views: 28989

Answers (3)

Croises
Croises

Reputation: 18671

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

Upvotes: 14

shubh jaiswal
shubh jaiswal

Reputation: 438

Answer by @Croises is correct but i have to add my full domain_url before $1

so final Ans is:

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^abc/(.*)/(.*)/ abc.php?id=$1&name=$2 [L]
RewriteRule ^abc/[^/]+/[^/]+(/.+)$ http://example.com/$1  [L]

Upvotes: -1

Forien
Forien

Reputation: 2763

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 (|)

Upvotes: 7

Related Questions