问题描述
我托管的网站只是一堆静态.html文件.我不想在URL中使用.html
文件扩展名,所以我添加了一个重写规则:
I'm hosting a website that's just a bunch of static .html files. I don't want to have the .html
file extension in the URLs, so I've added a rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+)$ /$1.html [L]
这很简单并且可以正常工作,但是我现在也希望将以.html
结尾的URL重定向(302)到规范路径,即没有文件扩展名.我尝试了以下方法:
This is simple enough and works fine, but I would now also like to redirect (302) URLs ending in .html
to the canonical path, i.e. without the file extension. I've tried the following:
RewriteCond %{REQUEST_FILENAME} \.html$
RewriteRule ^(.+)\.html$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+)$ /$1.html [L]
但是,这将导致无限的重定向循环.我怀疑这是因为第二条规则(内部重写)仍在触发第一条规则(外部重定向).
However, that leads to an endless redirect loop. I suspect that's because the second rule, the internal rewrite, is still triggering the first rule, the external redirect.
我还能怎么做到这一点?我浏览了所有的重写标志,并尝试了一些听起来很有希望的方法,但是我没有设法使它起作用.
How else can I achieve this? I've looked through all the rewrite flags and tried a bunch that sounded promising, but I haven't managed to make this work. How can I both rewrite from foo.html
to foo
and still do an internal rewrite from foo
to foo.html
?
推荐答案
有几个问题询问该过程如何作品.使用THE_REQUEST
变量:
There are several questions asking about how this procedure works. Use THE_REQUEST
variable:
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.html
RewriteRule \.html$ /%1 [R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ /$1.html [L]
这篇关于Apache:如何将foo.html重定向到foo并将foo重写到foo.html?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!