使用 Apache 将尾部斜杠添加到 URL

发布于 2024-10-03 12:06:57 字数 587 浏览 0 评论 0原文

我正在一个名为 testsite 的目录中工作,我希望所有 .php 扩展名都替换为尾部斜杠。我已经完成了一半,(例如)输入 http://mydomain.com/testsite/about 表示 http://mydomain.com/testsite/about.php 已加载。

不过,我现在希望 URL 显示为 ./about/,以便搜索引擎排名中只显示一个版本。

这是我的 .htaccess:

RewriteEngine On
RewriteBase /testsite/
RewriteRule ^()$ index.php [NC,L]
RewriteCond %{REQUEST_URI} !(^/?.*\..*$) [NC]
RewriteRule (.*)$ $1.php [NC]

另外,是否可以保留(并隐藏显示我传递的任何参数)?

非常感谢所有帮助!

I'm working in a directory called testsite and I want all .php extensions to be replaced by a trailing slash. I'm halfway there, in that (for example) entering http://mydomain.com/testsite/about means that http://mydomain.com/testsite/about.php is loaded.

However, I now want the URL to be displayed as ./about/ so that only one version shows up in search engine rankings.

Here's my .htaccess:

RewriteEngine On
RewriteBase /testsite/
RewriteRule ^()$ index.php [NC,L]
RewriteCond %{REQUEST_URI} !(^/?.*\..*$) [NC]
RewriteRule (.*)$ $1.php [NC]

Also, is it possible to preserve (and hide from display any parameters that I pass)?

All help is much appreciated!

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

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

发布评论

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

评论(2

秋叶绚丽 2024-10-10 12:06:57

尝试这些规则:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^$ index.php [L]
RewriteRule (.*)/$ $1.php [NC]

Try these rules:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^$ index.php [L]
RewriteRule (.*)/$ $1.php [NC]
嗼ふ静 2024-10-10 12:06:57

尝试将最后一条规则替换为:

RewriteRule (.*)$ $1.php [NC] [E=ORIGINAL_URL:$1]

Try replacing the last rule with:

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