以下 htaccess 很好地从我们的文件中删除了 .html 扩展名,例如:
/page1.html 重定向到/page1
但我们现在无法添加文件夹,因为它将/new-folder/index.html 文件重定向到/new-folder/index
有什么办法可以解决这个问题吗?
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.website\.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.website.co.uk/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
最佳答案
将删除 .html
的 301
规则更改为:
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))\.html[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# rewrite to dir/index.html if it exists
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/$ $1/index.html [L]
这会将 /new-folder/index.html
重定向到 /new-folder/
但会重定向 /new-folder/form.html
到 /new-folder/form
。
关于apache - 重写所有带有 .html 扩展名的 url(index.html 除外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57921685/