.htaccess 隐藏扩展名,同时强制 www

发布于 2024-11-07 06:58:37 字数 1009 浏览 0 评论 0原文

我有以下 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 技术交流群。

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

发布评论

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

评论(2

薄暮涼年 2024-11-14 06:58:37

您只需要更改规则的顺序即可。首先是外部重定向,然后是内部重定向。你的代码是这样的:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   Options +Indexes
   RewriteEngine On
   RewriteCond %{SCRIPT_FILENAME} !-d
   RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>

我已经测试过它并且在我这边工作得很好。

You just need to change the order of your rules. First external redirect then the internal one. Have your code like this:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

<IfModule mod_rewrite.c>
   Options +FollowSymLinks
   Options +Indexes
   RewriteEngine On
   RewriteCond %{SCRIPT_FILENAME} !-d
   RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>

I have tested it and works fine on my end.

暖树树初阳… 2024-11-14 06:58:37

在这种情况下,“foo”是什么,一个文件?如果是这样,这可行:

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

如果您想专门排除“foo”,

RewriteRule ^foo$ - [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

如果您尝试使用 Web 框架,那么他们可能希望您将所有非文件和非目录请求重定向到类似 index.php 的内容。

编辑:尝试交换并合并两个部分:

Options +FollowSymLinks
Options +Indexes

<IfModule rewrite_module>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  RewriteCond %{SCRIPT_FILENAME} !-f
  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>

What is "foo" in this case, a file? If so, this works:

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

If you want to exclude "foo" specifically,

RewriteRule ^foo$ - [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

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:

Options +FollowSymLinks
Options +Indexes

<IfModule rewrite_module>
  RewriteEngine On
  RewriteCond %{HTTP_HOST} !^www\.
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  RewriteCond %{SCRIPT_FILENAME} !-f
  RewriteCond %{SCRIPT_FILENAME} !-d
  RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文