重写所有带有.html扩展名的URL,但index.html除外

时间:2019-09-13 10:14:55

标签: apache .htaccess mod-rewrite

以下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]

1 个答案:

答案 0 :(得分:2)

更改301条规则,将.html删除:

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