htaccess 重写规则冲突
这里有一个真正的脑筋急转弯。 一切正常,直到我到达 mysite.com/index.php?page=XXX&parent_url=XXX&child_url=XXX 然后我收到此错误
警告:包括(浏览/12-oclock-bars/cbr-600-f4i.php)[function.include]:无法打开流:/home/leb/public_html/index中没有这样的文件或目录.php 第 167 行
警告:include() [function.include]:无法打开 'browse/12-oclock-bars/cbr-600-f4i.php' 进行包含 (include_path='.:/usr/lib/php' )在 /home/leb/public_html/index.php 第 167 行
但是前两行工作正常。为什么第三行不适用于我的网站。但是如果我注释掉前两行,那么第三行就可以了......我很困惑。
这是我的 HTACCESS 文件。任何帮助将不胜感激。
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^index.php?
RewriteRule ^(.*)/$ /index.php?page=$1 [L,NC]
RewriteRule ^(.*)/(.*)/$ /index.php?page=$1&parent_url=$2 [L,NC]
RewriteRule ^(.*)/(.*)/(.*)/$ /index.php?page=$1&parent_url=$2&child_url=$3 [L,NC]
-------------------------------------------------------- 修改了下面的新问题
在这里,第 3 行不起作用,但其他所有行都可以,如果我向上或向下更改其位置,其他行将停止工作。
RewriteRule ^(.*)/(.*)/(.*)/(.*)/$ /index.php?page=$1&parent_url=$2&child_url=$3product=$4 [L,NC]
RewriteRule ^(.*)/(.*)/(.*)/$ /index.php?page=$1&parent_url=$2&child_url=$3 [L,NC]
RewriteRule ^(.*)/(.*)/(.*)/$ /index.php?page=$1&parent_url=$2&product=$3 [L,NC] <---
RewriteRule ^(.*)/(.*)/$ /index.php?page=$1&parent_url=$2 [L,NC]
RewriteRule ^(.*)/$ /index.php?page=$1 [L,NC]
Having a real brain teaser here.
Everything works fine like it should until I get to
mysite.com/index.php?page=XXX&parent_url=XXX&child_url=XXX
Then I get this error
Warning: include(browse/12-oclock-bars/cbr-600-f4i.php) [function.include]: failed to open stream: No such file or directory in /home/leb/public_html/index.php on line 167
Warning: include() [function.include]: Failed opening 'browse/12-oclock-bars/cbr-600-f4i.php' for inclusion (include_path='.:/usr/lib/php') in /home/leb/public_html/index.php on line 167
But the first two lines are working fine. Why would the 3rd line not work for my site. But if I comment out the first two lines, than the third line works.... I'm so confused.
Here is my HTACCESS file. Any help would be greatly appreciated.
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^index.php?
RewriteRule ^(.*)/$ /index.php?page=$1 [L,NC]
RewriteRule ^(.*)/(.*)/$ /index.php?page=$1&parent_url=$2 [L,NC]
RewriteRule ^(.*)/(.*)/(.*)/$ /index.php?page=$1&parent_url=$2&child_url=$3 [L,NC]
-------------------------------------------
Revised with new problem below
Here, Line 3 won't work, but all the others do, If I change its position up or down, the others stop working.
RewriteRule ^(.*)/(.*)/(.*)/(.*)/$ /index.php?page=$1&parent_url=$2&child_url=$3product=$4 [L,NC]
RewriteRule ^(.*)/(.*)/(.*)/$ /index.php?page=$1&parent_url=$2&child_url=$3 [L,NC]
RewriteRule ^(.*)/(.*)/(.*)/$ /index.php?page=$1&parent_url=$2&product=$3 [L,NC] <---
RewriteRule ^(.*)/(.*)/$ /index.php?page=$1&parent_url=$2 [L,NC]
RewriteRule ^(.*)/$ /index.php?page=$1 [L,NC]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如 @jeroen 所评论的,规则 2 和 3 的传入 URL 没有区别。尽管它们应该访问的预期页面不同,但您在两个规则中检查的传入 URL 仍然是
/a/ b/c
。如果 URL 中有一些指示符,例如规则 3,作为产品:随着该更改,正如您当前的规则一样,该规则必须位于当前的第二条规则之前。
另外,当匹配这样的部分时,我发现使用
([^\/]*)
而不是(.*)
更容易。这样,该组就会匹配/
之前的所有内容,并且您会注意到对规则顺序的依赖减少了。希望这有帮助。
As @jeroen commented, there is no difference in the incoming URLs for rules 2 and 3. Although the intended page they are supposed to go to is different, the incoming URLs you are checking for in both rules is still
/a/b/c
. If you had some indicator in the URL, say for Rule 3, being a product:With that change, as your rules are currently, that rule would have to come before your current 2nd rule.
Also, when matching sections like this, I find it much easier to use
([^\/]*)
instead of(.*)
. This way, the group matches everything up until the/
and you will notice less dependence on the ordering of your rules.Hope this helps.
第 2 行和第 3 行查找相同的模式,因此如果匹配,执行将在第 2 行终止,并且永远不会到达第 3 行。
Line 2 and line 3 look for the same pattern, so execution will terminate at line 2 and never reach line 3 in case of a match.
我得出的结论是,在 PHP 中处理 url 更容易。
您可以在 htaccess 中使用以下代码传递整个路径:
在 PHP 中,您可以分解路径:
I have come to the conclusion that it is easier to deal with the url in PHP.
You can pass the whole path with the following code in htaccess:
In PHP you can explode the path:
尝试将 (.*) 替换为 ([^/]+):
并对每一行执行相同的操作。
Try replacing (.*) by ([^/]+):
And do the same thing for every line.