Excluding Images from the mod_rewrite Rule

Suddenly, my htaccess script changes the URLs of images to adversely affect any image with a "portfolio /" in its URL.

Is there any way to exclude images from this particular rule?

redirect 301 "/sitemap.xml" http://www.example.com/sitemap.php RewriteEngine On RewriteCond %{REQUEST_URI} !portfolio/project RewriteCond %{REQUEST_URI} /.*portfolio/.*$ [NC] RewriteRule ^(.*)portfolio(.*)$ /$1portfolio/project$2 [R=301,L] RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^/sitemap.php RewriteRule ^(.*)$ /index.php/$1 [L] 
+4
source share
2 answers

For the first part (first rewrite rule) try

 RewriteCond %{REQUEST_URI} !portfolio/project RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$ [NC] RewriteCond %{REQUEST_URI} /.*portfolio/.*$ [NC] RewriteRule ^(.*)portfolio(.*)$ /$1portfolio/project$2 [R=301,L] 

I'm not sure why you are using RewriteCond $1 !\.(gif|jpe?g|png)$ [NC] in the second rewriter.

RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png)$ [NC] means that you do not want to modify the portfolio in the portfolio / project for URLs ending with some permitted extension. [NC] (case insensitive) is used to skip JPG, GIF, PnG, etc. Besides,

+6
source

If your goal is to completely exclude images from the rewrite rules, try adding the following line as your first rule:

  RewriteRule ^.*\.(gif|jpe?g|png)$ - [L] 

It just stops the movement of the rules engine and does not rewrite the URL if it matches the pattern above.

+2
source

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


All Articles