如果不存在,mod_rewrite规则附加index.html

时间:2012-09-14 09:12:45

标签: apache mod-rewrite apache2

index.html附加到以正斜杠结尾的任何网址的mod_rewrite规则是什么?规则应保留存在的任何查询字符串。我无法使用DirectoryIndex指令,因为index.html文件在文件系统上实际上不存在,但是底层网站框架需要这些文件。

一些示例网址和所需结果如下所示:

http://example.com/                   -> http://example.com/index.html
http://example.com/?a=1               -> http://example.com/index.html?a=1
http://example.com/foo/               -> http://example.com/foo/index.html
http://example.com/foo/?b=2           -> http://example.com/foo/index.html?b=2
http://example.com/foo/index.html     -> http://example.com/foo/index.html
http://example.com/foo/index.html?c=3 -> http://example.com/foo/index.html?c=3

1 个答案:

答案 0 :(得分:7)

除非正在修改查询字符串本身,否则查询字符串会自动被mod_rewrite追加。这应该是你需要的:

RewriteEngine On
RewriteRule ^/?$ /index.html [L,R=301]
RewriteRule ^/?(.*)/$ /$1/index.html [L,R=301]

这样,当有人请求以/结尾的任何内容时,请将浏览器重定向到最后带有index.html的同一网址。空白URI是一种特殊情况(第一条规则)。如果您不需要重定向浏览器,只需从方括号中删除,R=301

相关问题