HTML5 Boilerplate .htaccess重写为index.php

时间:2011-11-09 04:31:22

标签: html5 .htaccess indexing rewrite boilerplate

我的问题是:

如果我在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文件中的一些规则吗?如果错了,应该添加什么?

1 个答案:

答案 0 :(得分: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文件的开头,而不是其他规则之后。