我看到所有这些用于删除 .html 扩展名的脚本似乎都无法通过 SSL 工作。我用新脚本解决了这个问题,但现在我正在创建一个 2 步重定向链,出于 SEO 的原因我不喜欢它 - 在你的帮助下 :) 我希望让它回到重定向步骤/跃点。
我的脚本是
RewriteBase /
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.carpro.ro/$1 [R=301,L]
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.*)\.html$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
但它似乎创建了一系列重定向。
有没有更好的版本
- 网站所有页面的 301 http 到 https
- 将非 WWW 重定向到 WWW
- 删除 .html 扩展名并且不留下尾随/
上面的脚本可以工作,但是我不喜欢重定向级联
http://www.domain.com/something.html 执行 301 重定向 https://www.domain.com/something.html 然后再次 301 重定向 https://www.domain.com/something
而不是 http://www.domain.com/something.html 一个重定向到 https://www.domain.com/something
有什么优化的想法吗?
最佳答案
试试这段代码:
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://www.carpro.ro%{REQUEST_URI} [NE,R=302,L]
# Force WWW prefix
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=302,L]
# Remove .html extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.html
RewriteRule (.+?)\.html$ /$1 [L,R=302,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+?)/?$ $1.html [NC,L]
关于.htaccess - SSL 并通过 HTACCESS 删除扩展名 .html,无需重定向链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27343496/