如何删除和重定向所有 .php 扩展名、重定向“www”以及删除尾部斜杠?
我一直在尝试从网站上的所有页面中删除“.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 htaccess URL 重写隐藏 PHP
URL 重写初学者
如何在 PHP 中进行 URL 重写?
URL 重写工具
检查以上网址。在这些教程中,您可以了解如何重写 URL。
Hide PHP using htaccess URL rewriting
URL Rewriting for beginners
How to do URL re-writing in PHP?
URL Rewriting tool
Check above urls. In these tutorials you can see how to rewrite URLs.
我今天刚刚对尾部斜杠有同样的问题。这就是我最终在 .htaccess 文件中解决这个问题的方法(重要的是 DirectorySlash 和第一个重写规则):
因此,如果您输入 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):
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