尾部斜杠和首字母 www

发布于 2024-08-21 16:07:53 字数 493 浏览 2 评论 0原文

我有这个 htaccess:

  RewriteEngine On

  # redirect with www
  RewriteCond %{HTTP_HOST} ^mydomain [NC]
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [R=301,L]

  # add .php internally
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ $1.php [L,QSA]

所以我的 .php 文件可以在没有 .php 扩展名的情况下调用。

但我希望仅用尾部斜杠来调用它们。因此,当未给出尾部斜杠时,应附加 301。我遇到的问题是,这给我带来了初始 www 和 .php 扩展本身的问题(有时它会递归地添加 .php)。

怎么办呢?

谢谢!

I have this htaccess:

  RewriteEngine On

  # redirect with www
  RewriteCond %{HTTP_HOST} ^mydomain [NC]
  RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [R=301,L]

  # add .php internally
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ $1.php [L,QSA]

So my .php files can be called without the .php extension.

But I'd like them to be called only with a trailing slash. So when this trailing slash is not given, it should be appended with a 301. The problem I have is that this is giving me problems with the initial www, and the .php extension itself (sometimes it is adding recursively .php).

How can it be done?

Thanks!

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

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

发布评论

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

评论(1

魂归处 2024-08-28 16:07:53

我认为你需要在最后一个重写规则之前添加类似的内容,以避免重写已经以 .php 结尾的 URI

RewriteCond %{REQUEST_URI} !\.php$

I think you need to add something like this before your last rewrite rule, to avoid rewriting URIs that already end in .php

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