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

Joh*_*ley 3 apache mod-rewrite apache2

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

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

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
Run Code Online (Sandbox Code Playgroud)

Jon*_*Lin 7

除非正在修改查询字符串本身,否则查询字符串将自动附加到mod_rewrite.这应该是你需要的:

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

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