.htaccess RewriteRule also overwrites css, js and images files. how to ignore them?

I want that when the user types http://example.com/abc/123/jon/ into the URL, this file actually displays http://example.com/abc.php?id=123&name=jon , and this is easy to do by adding these lines to the htaccess file:

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

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

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

and images of type: img / logo.png

but because of my .htaccess file it is looking 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 have already tried many similar answers here, but none of them work for me. I am looking for a .htaccess solution for this.

+5
source share
3 answers

You can use this:

 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 in the root directory, you can add the path to the last $1 . Now this is: /css/style.css with /abc/xxx/yyy /css/style.css

+9
source

Just add one line before the RewriteRule :

 RewriteCond %{REQUEST_FILENAME} !-f 

This means that if your uri request points to an existing file, then the next RewriteRule will not start.

If you need another way, then you need to do this:

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

You can add additional extensions, separated by a pole ( | )

+2
source

The answer is @Croises, but I have to add my domain_url domain to $ 1

, so the final Ans:

 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] 
-1
source

Source: https://habr.com/ru/post/1210986/


All Articles