如何删除和重定向所有 .php 扩展名、重定向“www”以及删除尾部斜杠?

发布于 2024-12-17 06:49:36 字数 518 浏览 0 评论 0原文

我一直在尝试从网站上的所有页面中删除“.php”,同时还使用 .htaccess 从 URL 中删除所有尾随斜杠和“www”。我能够同时使用其中的一两个,但不是全部。我真的很感激任何帮助。

例如:

  • example.com/ 到 example.com
  • www.example.com 到 example.com
  • example.com/index 到 example.com
  • example.com/index.php 到 example.com
  • example.com/chatroom.php 到 example。 com/chatroom
  • example.com/directory/ 到 example.com/directory

我想确保旧的 URL 重定向到新的 URL,并且我没有任何与 PHP 文件共享名称的目录。在我的 .htaccess 文件中,我还有阻止特定引荐来源网址并指定 403 和 404 错误的代码。 非常感谢!

I've been trying to remove ".php" from all of the pages on my website, whilst also removing all trailing slashes and "www" from URLs using .htaccess. I have been able to use one or two of these at the same time, but not all. I would really appreciate any help.

For example:

  • example.com/ to example.com
  • www.example.com to example.com
  • example.com/index to example.com
  • example.com/index.php to example.com
  • example.com/chatroom.php to example.com/chatroom
  • example.com/directory/ to example.com/directory

I would like to ensure that the old URLs redirects to the new ones, and I don't have any directories that share names with PHP files. In my .htaccess file I also have code blocking particular referrers and specifying 403 and 404 errors.
Thank you very much!

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

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

发布评论

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

评论(2

随心而道 2024-12-24 06:49:36

我今天刚刚对尾部斜杠有同样的问题。这就是我最终在 .htaccess 文件中解决这个问题的方法(重要的是 DirectorySlash 和第一个重写规则):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Do not automatically add a slash for existing directories
DirectorySlash Off


#No trailing slash. This means that an URL entered with trailing / will change to an URL without trailing /
#The !-d rule can be included so that any URLs to existing directories are not changed
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

#Allow URL's which do not have the php file extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^  %{REQUEST_FILENAME}.php [L]


#Redirect any file which does not exist to the index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [R=301,L]

</IfModule>

因此,如果您输入 URL example.com/index/ ,它将重定向到 example.com/index。如果您输入 example.com/chatroom (并且存在 chatroom.php),则 URL 中仍然包含 example.com/chatroom,但您的 PHP 脚本接收 example.com/chatroom.php

您必须添加的是以下两种情况将index.php重定向到/并删除www

我所包含的(我不知道您是否需要它)是将任何不存在的文件重定向到index.php的最后一条规则

I just had the same question today regarding the trailing slashes. This is how I ended up solving it in my .htaccess file (important are DirectorySlash and the first rewrite rule):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Do not automatically add a slash for existing directories
DirectorySlash Off


#No trailing slash. This means that an URL entered with trailing / will change to an URL without trailing /
#The !-d rule can be included so that any URLs to existing directories are not changed
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

#Allow URL's which do not have the php file extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^  %{REQUEST_FILENAME}.php [L]


#Redirect any file which does not exist to the index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [R=301,L]

</IfModule>

So if you type as URL example.com/index/ it would redirect to example.com/index. If you type example.com/chatroom (and an chatroom.php exists) it still has example.com/chatroom in the URL but your PHP script receives example.com/chatroom.php

What you would have to add are the two cases to redirect index.php to / and to remove the www

What I have included (and I don't know if you need it) is the last rule which redirects any non existing files to index.php

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