Exception with .htaccess (image broken)

2019-06-05 22:55发布

问题:

I'm using:

RewriteRule ^(.*)$ index.php?abrir=$1
RewriteRule ^(.*)/(.*)/(.*)$ index.php?abrir=$1&id=$2&pagina=$3

and I have the image link:

/data_livro/capa_livro/af58e898f91509b.jpg

How I can add a exception for show the image? Its broke...

My full .htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^/]+)$ index.php?abrir=$1
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?abrir=$1&id=$2&pagina=$3

RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]

ErrorDocument 400 index.php
ErrorDocument 401 index.php
ErrorDocument 403 index.php
ErrorDocument 404 index.php
ErrorDocument 504 index.php
ErrorDocument 505 index.php
DirectoryIndex index.php

回答1:

I find that using .* in a rewrite rule is too general. I tend to use more specific character classes:

RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?abrir=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?abrir=$1&id=$2&pagina=$3

This doesn't rewrite any request with a dot in it, so specific file names (/styles/my.css, /images/example.jpg) get ignored. Also, I like to add /? to allow an optional trailing slash, but that's a personal preference :-).



回答2:

If I'm understanding this correctly, ading the following ahead of your RewriteRules should solve the problem:

RewriteCond %{REQUEST_FILENAME} !-f

However, I'd also suggest modifying your RewriteRules to look like:

RewriteRule ^([^/]+)$ index.php?abrir=$1
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?abrir=$1&id=$2&pagina=$3