2015-09-30 71 views
0

我有一个.htaccess文件应该将所有请求重定向到index.php?url=$1,除非请求指向图像或特定文件。如果这些文件夹位于public/文件夹的某个位置,则应将其送达。如果请求的文件是public/文件夹以外的图像,则将其重定向到默认图像,并且如果请求的文件是HTML,JS,CSS,SWF文件并且不在public/文件夹中,则应发送一个类似404-not found的文件背部。 一切正常,除非我要求/public的图片或让我们说public/images它们也被重定向到默认图片。我已经尝试了一切,即使是%{ENV:REDIRECT_STATUS},但仍然一样。任何想法我怎么能让循环停止如果文件已在第11行服务?重写循环不会停止

下面是.htaccess文件:

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    SetEnv HTTP_MOD_REWRITE on 
    RewriteBase /wsproject/ 

    Options All -Indexes 
    DirectoryIndex index.php 

    RewriteCond %{REQUEST_URI} ^public/.*\.(html|css|js|swf|jpe?g|png|gif|bmp|ico)$ [NC] 
    RewriteCond %{REQUEST_FILENAME} -f 
    RewriteRule^- [L] 

    RewriteCond %{REQUEST_URI} \.(jpe?g|png|gif|bmp|ico)$ [NC] 
    RewriteCond %{REQUEST_FILENAME} -f 
    RewriteRule^public/errors/img-not-found.png [L] 

    RewriteCond %{REQUEST_URI} \.(html|css|js|swf)$ [NC] 
    RewriteRule^- [R=404,L] 

    RewriteCond %{REQUEST_URI} !^public/.*$ [NC] 
    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] 
</IfModule> 

我使用的代码从原来的答案,你可以在这里看到:Apache Rewrite module, complex rules

回答

0

使用THE_REQUEST变量,而不是作为REQUEST_URI你最后的规则是重写一切到/index.php并更改值REQUEST_URI

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    SetEnv HTTP_MOD_REWRITE on 
    RewriteBase /wsproject/ 

    Options All -Indexes 
    DirectoryIndex index.php 

    RewriteCond %{THE_REQUEST} /public/.+?\.(html|css|js|swf|jpe?g|png|gif|bmp|ico)\s [NC] 
    RewriteCond %{REQUEST_FILENAME} -f 
    RewriteRule^- [L] 

    RewriteCond %{THE_REQUEST} \.(jpe?g|png|gif|bmp|ico)\s [NC] 
    RewriteCond %{REQUEST_FILENAME} -f 
    RewriteRule^public/errors/img-not-found.png [L] 

    RewriteCond %{THE_REQUEST} \.(html|css|js|swf)\s [NC] 
    RewriteRule^- [R=404,L] 

    RewriteCond %{THE_REQUEST} !/public/ [NC] 
    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] 
</IfModule>