htaccess 中的重定向不适用于 2 条规则
htaccess 在第一行工作正常,但在第二行工作不正常。如果我删除第一行,那么第二行就可以正常工作。 为什么两条线不能一起工作?
RewriteRule ^(.*) /single-services.php?slug=$1
RewriteRule ^blog-(.*)$ single-blog.php?slug=$1
htaccess working fine with first line but not working on second line. if I remove first line than second line works fine.
Why both lines not working together?
RewriteRule ^(.*) /single-services.php?slug=$1
RewriteRule ^blog-(.*)$ single-blog.php?slug=$1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需更改它们在文件中的执行顺序即可。由于您的第一条规则涵盖
.*
这是一个贪婪匹配,所以这就是为什么它总是执行第一个规则。由于我们现在更改了规则顺序,因此当请求以 blog 开头的 URI 时,.*
规则将不会提供服务。此外,我还在您的规则中添加了必要的标志,这将有助于规则的顺利执行。在测试您的网址之前,请确保清除浏览器缓存。
注意:另外,在您重写 php 文件(位于规则右侧)之前,我已经删除了
/
,以防您的文件中的 3 个(单个-blog.php、single-services.php 和 .htaccess) 存在于根文件夹中,那么您可以在single-blog.php
和single- 之前添加
/
services.php 中两条规则。Simply change their order of execution in your file. Since your first rule covers
.*
which is a greedy match so that's why its executing always the first one. Since we changed the sequence of rules now now when request comes for URI starts with blog it will NOT be served by.*
rule.Also I have added necessary flags to your rules which will help in smooth execution of rules. Please make sure to clear your browser cache before testing your URLs.
NOTE: Also I have removed
/
before your rewriting to your php file(on right side of rules) in case 3 of your files(single-blog.php, single-services.php and .htaccess) are present in root folder then you can add/
beforesingle-blog.php
andsingle-services.php
in both Rules.