mod_rewrite:如何根据“可用性”匹配目录或根目录中的文件?

发布于 2024-10-06 12:58:39 字数 623 浏览 0 评论 0原文

您好,老听众,第一次来电!
感谢大家分享的出色建议。

我设置了这些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄断弥空 2024-10-13 12:58:39

找到了。作为参考,这是完整的规则集:

RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/.]+)/([^/.]+)/?$ /$1/$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1--$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/?$ $1.php?%{QUERY_STRING} [L]

Found it. For reference, this is the complete set of rules:

RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/.]+)/([^/.]+)/?$ /$1/$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ $1--$2.php?%{QUERY_STRING} [L]
RewriteRule ^([^/.]+)/?$ $1.php?%{QUERY_STRING} [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文