进行匹配后,.htaccess重写会继续进行 [英] .htaccess rewrite keeps going after a match is made

查看:82
本文介绍了进行匹配后,.htaccess重写会继续进行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将以htaccess和mod_rewrite不是我最好的领域为开头.我已经编写了一个供个人使用的php MVC框架,并且在文档根目录中,有一个名为"public"的文件夹,用于css,js,图像等.每个HTTP请求都针对"public"中的文件进行检查,如果存在, ,它发送该文件;否则,它将重定向到该框架的index.php.

I'll preface this with the fact that htaccess and mod_rewrite aren't my best areas. I've written a php MVC framework for personal use, and in the document root I have a folder called "public" for css, js, images, etc. Every HTTP request is checked against the files in "public" and if it exists, it sends that file; otherwise it redirects to index.php for the framework.

规则有效,但是如果在"public"中进行匹配,则仍会路由到index.php.如果我注释掉重定向到index.php的最后一行,则CSS和JS会正确加载.在完成公开"匹配后,我无法停止处理.

The rules work, but if a match is made in "public," it still routes to index.php. If I comment out the last line which redirects to index.php, the css and js load properly. I can't get it to stop processing after the "public" match is made.

预先感谢

Options +FollowSymLinks
RewriteEngine On

#If file exists as public resource
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule ^(.*) %{DOCUMENT_ROOT}/public/$1 [NC,L]


#Redirect everything else to index.php
RewriteRule ^(.*) index.php [NC,L]

推荐答案

您需要添加测试以查看请求是否已指向公共目录,并且仅在未触发的情况下触发RewriteRule:

You need to add a test to see whether the request has already been pointed to the public directory, and only trigger the RewriteRule if not:

RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^.*$ index.php

请注意,[L](最后一个")标志经常被误解.它将停止在当前通过中对请求进行进一步测试和修改,但是它无法阻止Apache从一开始就通过整个规则集运行新修改的请求(这将发生,因为已应用.htaccess规则,从顶部到每个新的或修改的请求).因此,您需要添加RewriteCond指令来检查您是否没有处理在规则集上一遍中指向公共文件夹的请求.

Note that the [L] ("last") flag is often misunderstood. It stops the request being further tested and modified in the current pass, but it cannot stop Apache running the newly modified request through the entire ruleset from the start (which will happen because the .htaccess rules are applied, from the top, to every new or modified request). So you need to add the RewriteCond directive to check that you're not dealing with a request which was pointed to the public folder in the previous pass of the ruleset.

请参阅有关Apache 2.4文档的最后"标志有关更多详细信息. [请注意,Apache 2.4提供了[END]标志,它将停止对请求进行另一遍重写处理的请求,但是该新标志在Apache 2.2及更早版本中不可用.]

See the Apache 2.4 documentation for the "last" flag for more details. [Note that Apache 2.4 offers the [END] flag which will stop requests being subjected to another pass of rewrite processing, but this new flag is not available in Apache 2.2 and earlier.]

这篇关于进行匹配后,.htaccess重写会继续进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆