.htaccess 隐藏扩展名,同时强制 www
我有以下 htaccess 文件,它隐藏了 php 扩展名并强制所有 URL 使用 www:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
这在大多数情况下都是完美的,但有一个问题。当用户尝试访问 http://example.com/foo (或代替“foo”时,无论其他页面)它重定向到 http://www.example.com/foo.php 。
如何使其重定向到 http://www.example.com/foo ,即没有.php?
I have the following htaccess file, which hides the php extension and forces all URLs to use www:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
For the first part the credit goes to this answer and the second one to this answer.
This works perfect in most cases, however there is one problem. When a user tries to go to http://example.com/foo (or instead of "foo", whatever other page) it redirects to http://www.example.com/foo.php .
How can I make it redirect to http://www.example.com/foo , that is, whithout the .php?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需要更改规则的顺序即可。首先是外部重定向,然后是内部重定向。你的代码是这样的:
我已经测试过它并且在我这边工作得很好。
You just need to change the order of your rules. First external redirect then the internal one. Have your code like this:
I have tested it and works fine on my end.
在这种情况下,“foo”是什么,一个文件?如果是这样,这可行:
如果您想专门排除“foo”,
如果您尝试使用 Web 框架,那么他们可能希望您将所有非文件和非目录请求重定向到类似 index.php 的内容。
编辑:尝试交换并合并两个部分:
What is "foo" in this case, a file? If so, this works:
If you want to exclude "foo" specifically,
If you're trying to use a web framework, then they probably want you to redirect all non-file and non-directory requests to something like index.php.
Edit: Try swapping and consolidating the two sections: