gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:29:31 24 4
gpt4 key购买 nike

index.html 附加到任何以正斜杠结尾的 URL 的 mod_rewrite 规则是什么?该规则应保留存在的任何查询字符串。我无法使用 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

最佳答案

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

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

这使得当有人请求以 / 结尾的任何内容时,将浏览器重定向到末尾带有 index.html 的同一 URL。空白 URI 是一种特例(第一条规则)。如果不需要重定向浏览器,只需将方括号中的 ,R=301 去掉即可。

关于apache - mod_rewrite 规则附加 index.html 如果不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12421432/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com