mod_rewrite:如何根据“可用性”匹配目录或根目录中的文件?
您好,老听众,第一次来电!
感谢大家分享的出色建议。
我设置了这些 mod_rewrite 规则:
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1--$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/?$ $1.php?%{QUERY_STRING} [L]
它们使 /company/services/ 重定向到 company--services.php。或者 /company/ 到 company.php。作品完美。
但现在我想要另一个规则,如果我将 services.php 放入物理目录 /company/ 中,它将匹配并重定向该目录。如果失败,请查找我的初始规则。 (如果失败,则返回 404。)
我认为这会像包含:一样简单,
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1/$2.php?%{QUERY_STRING} [L]
但事实并非如此。它将返回 404。我有点困惑,因为这违背了我相信 mod_rewrite 的工作方式(如果规则不匹配,请转到下一个。)
谢谢您的指点!
Hello, long time listener, first time caller here!
Thank you for the excellent advice you all share.
I have these mod_rewrite rules set up:
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1--$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/?$ $1.php?%{QUERY_STRING} [L]
They make /company/services/ redirect to company--services.php. Or /company/ to company.php. Works perfect.
But now I'd like to another rule that if I were to put services.php inside physical directory /company/ it will match and redirect that. And if failing that, look for my initial rule. (And failing that, return 404.)
I figured it would be as simple as including:
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1/$2.php?%{QUERY_STRING} [L]
but not so. It will returns a 404 instead. I'm a bit stumped as this goes against how I believed mod_rewrite to work (if a rule does not match, go to the next one.)
Thank you for any pointers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了。作为参考,这是完整的规则集:
Found it. For reference, this is the complete set of rules: