apache - 重写所有带有 .html 扩展名的 url(index.html 除外)

标签 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]

最佳答案

将删除 .html301 规则更改为:

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/

相关文章:

apache - 在 MacOS 10.11 上编译 http 2.4.16 时出现 "OpenSSL version is too old"

regex - 为特定网址强制使用 HTTPS

apache - 如何使用 apache 提供静态文件?

apache - 我需要在 .htaccess 文件中重复 RewriteCond 吗?

apache - 为 CloudFlare 用户强制使用 .htaccess 的 HTTPS 和非 WWW

php - mod_rewrite {QUERY_STRING}

java - Apache POI 在多线程中的 tmp 目录上获取 java.io.IOException

apache - 如何更改在 DOCKER 容器内运行的 apache 的 httpd.conf 并重新启动 apache

apache - 使用 .htaccess 将动态(可变)子域重定向到 Controller

.htaccess - htaccess 子域重定向与最后一个 url 参数