HTML5 Boilerplate .htaccess 重写为 index.php

标签 html .htaccess indexing url-rewriting 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 文件中的一些规则吗?如果错误,应该添加什么?

最佳答案

您在上面发布的规则已经存在于 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."下的规则。以这种方式在 URL 的开头:

<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 文件的开头,放在其他规则之前,而不是之后。

关于HTML5 Boilerplate .htaccess 重写为 index.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8060363/

相关文章:

C++:写入/读取无效/超出绑定(bind)的数组索引?

html - 模板站点未正确导入字体和图标

python - 使用 Selenium 抓取 Google map 的网页使用了太多数据

.htaccess - 为 N 个查询字符串重写 url

php - SSL 网站在子 URL 中附加 https

mysql - sql - @curRow 基于列

javascript - 将外部库与 HyperHTML 结合使用

php - 使用 PHP 的 Apache 速度

apache - 使用 .htaccess 将一个文件夹重定向到另一个文件夹,但保留原始 URL 并阻止对所有其他文件夹的访问

mysql - 应该添加哪些索引来加快海量InnoDB表的查询速度?