.htaccess - SSL 并通过 HTACCESS 删除扩展名 .html,无需重定向链

标签 .htaccess redirect ssl

我看到所有这些用于删除 .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]

但它似乎创建了一系列重定向。

有没有更好的版本

  1. 网站所有页面的 301 http 到 https
  2. 将非 WWW 重定向到 WWW
  3. 删除 .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/

相关文章:

regex - 仅从搜索引擎中阻止 2 个域中的 1 个

C# SslStream 抛出 "Verification failure during handshake"

php - htaccess 重定向中的元音变音

python - Django 内针对 www 子域的 301 重定向

jquery - 使用 jquery 捕获输入事件并重定向

ssl - 检查 haproxy 在运行时使用哪个证书的方法?

powershell - 使用 x509 证书通过 PowerShell 解密使用 OpenSSL 加密的文本

.htaccess - https 通过 htaccess 更改文档根目录

Apache RewriteRule 无限循环

.htaccess - htaccess : Remove and disable file ending