mod_rewrite 初学者:将文件映射到 php 文件

发布于 2024-08-23 06:13:57 字数 340 浏览 4 评论 0原文

我正在尝试编写一个简单的规则,以便:

/page 隐式路由到 /page.php。相反,我希望对 /page.php 的任何直接访问都在地址栏中重定向到 /page

目前我有:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

但这不能处理第二种情况。我该如何修改它?

I'm trying to write a simple rule such that:

/page gets implicitly routed to /page.php. Inversely, I'd like any direct access to /page.php to be redirected in the address bar to /page.

Currently I have:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

But that does not handle the 2nd case. How can I modify it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

峩卟喜欢 2024-08-30 06:13:57

您必须先处理第二条规则,然后再处理第一条规则。试试这个:

RewriteEngine on
RewriteRule ^(.*)\.php$ http://domain.com/$1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php

You have to treat your second rule before your first one. Try this:

RewriteEngine on
RewriteRule ^(.*)\.php$ http://domain.com/$1/ [R=301,L]

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