简体   繁体   中英

.htaccess exclude images from rewrite condition

I've been trying to exclude png and jpg files from being excluded from the rewrite condition. Currently, I have a set of pictures that are being presented as broken images. What is causing the error is a rewrite rule in my .htaccess. That rule overwrites the URL of a folder so it will appear with a different name in the URL, instead of /volley it appears as JeepVolleyballChampionship. I've tried to get rid off the last part that redirects to the home page if a directory doesn't exist, but nothing seems to work. I've also tried to include an exclusion of files with extensions of png and jpg from the rewrite block that rewrites the directory...

What I assume is that I'm not implementing that exclusion correctly. My page still loads fine, except with the broken images. Can you provide insight of what I'm doing wrong in this block?

   <IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^JeepVolleyballChampionship(.*)$ /volley [L,QSA]
</IfModule>

My whole .htaccess is provided here:

#the non-existent JeepVolleyballChampionship folder leads to the volley folder without showing it or the extension


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

DirectorySlash Off
RewriteEngine on  
#try to make it so that /volley/ is eliminated from folders and name changes but it is still under this structure
RewriteRule ^volley$ /volley/index.php [L,E=LOOP:1]

RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^volley/$ /volley [R=301,L]   

RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^volley/index.php$ /volley [R=301,L]     

#the non-existent JeepVolleyballChampionship folder leads to the volley folder without showing it or the extension
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^JeepVolleyballChampionship(.*)$ /volley [L,QSA]
</IfModule>

#non-existent folder to default root
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ / [L,QSA]
</IfModule>

jpg and jpeg are treated as separate extensions here. Add jpeg as well on the line no 3. It may fix your issue, if your extension is "jpeg". It will look as follows:

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.jpeg|\.gif)$ [NC]

You should not be entering "RewriteEngine on" multiple times, and you also need to be careful about the order of your rules. Off the top of my head I think all you should need is this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/volley /JeepVolleyballChampionship [R=301]

The !-f condition will prevent the images from being rewritten (along with all other files) so long as they exist, this is the canonical way to exclude all existing files like js, css, etc.

Then the pattern in the rule will only rewrite URLs that start with /volley, so you do not need a condition to match it -- in fact, the rule pattern is checked BEFORE the conditions, so matching there is faster than matching everything and then checking the conditions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM