2

我的问题是:

如果我在 HTML5 Bilerplate 的 .htaccess 文件底部添加以下代码

<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^ index.php [L]
</IfModule>

这会将所有 REQUEST_URI 发送到我的 index.php 以便我可以处理它们,但它会破坏上面已经定义的 .htaccess 文件中的一些规则吗?如果它错了,应该添加什么?

4

1 回答 1

0

您在上面发布的规则已经存在于 HTML5 样板的 .htaccess 中(在标题下# Built-in filename-based cache busting

# Uncomment to enable.
# <IfModule mod_rewrite.c>
#   RewriteCond %{REQUEST_FILENAME} !-f
#   RewriteCond %{REQUEST_FILENAME} !-d
#   RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]
# </IfModule>

您可以通过删除每行开头的“#”来取消注释它们。并更改第 5 行以允许您对 favicon.ico 的规则:

RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif|ico)$ $1.$3 [L]

编辑:要将所有请求定向到 index.php,我# Suppress or force the "www." at the beginning of URLs以这种方式修改了规则:

<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
  RewriteCond %{REQUEST_FILENAME} !^(.+)\.(js|css|png|jpg|gif|ico)$
  RewriteRule ^(.*)$ index.php [NC]
</IfModule>

一般来说,您应该将自己的规则放在 .htaccess 文件的开头,放在其他规则之前,而不是在这些规则之后。

于 2011-11-09T07:34:57.853 回答