如何在 php 中制定重写规则?

发布于 2024-08-26 04:42:31 字数 224 浏览 2 评论 0原文

RewriteEngine on
Rewriterule ^(.*).htm $1.php

当我尝试访问每个 php 页面时,这工作正常

但是如果页面是 about.php ,那么我怎么能使它 RewriteRule ^/somepage $ /somepage.php (它不起作用),

URL 应该是关于/(目录类型)

RewriteEngine on
Rewriterule ^(.*).htm $1.php

This works fine when i try to access every php page

But how could i make it RewriteRule ^/somepage $ /somepage.php (its not working )

if the page is about.php the url should be about/ (directory type)

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

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

发布评论

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

评论(3

倾城泪 2024-09-02 04:42:31

Jan 的答案的另一种选择是:

RewriteRule ^/about/?$ /about.php

无论他们是否记得最后的 / ,都会重写它。

Alternatively to Jan's answer:

RewriteRule ^/about/?$ /about.php

will rewrite it whether they remember the final / or not.

乄_柒ぐ汐 2024-09-02 04:42:31

如果要在 .htaccess 文件中使用该规则,则需要从模式中删除本地路径前缀,因为它是在测试模式之前删除的。如果是根目录,则为 /。所以试试这个:

RewriteRule ^somepage/$ /somepage.php

对于任意路径段:

RewriteRule ^([^/]+)/$ /$1.php

If you want to use the rule in a .htaccess file, you need to remove the local path prefix from the pattern as it is removed before testing the pattern. In case of the root directory that is the /. So try this:

RewriteRule ^somepage/$ /somepage.php

And for arbitrary path segments:

RewriteRule ^([^/]+)/$ /$1.php
生死何惧 2024-09-02 04:42:31
RewriteRule ^/about/$ /about.php
RewriteRule ^/about/$ /about.php
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文