【问题标题】:How to fix my .htaccess to redirect to https://www?如何修复我的 .htaccess 以重定向到 https://www?
【发布时间】:2019-04-30 09:40:20
【问题描述】:

我想了解我的错误在哪里。我的 .htaccess 有问题,我想将所有基本条目重定向到 https://www.example.com

http://example.com to https://www.example.com
http://www.example.com to https://www.example.com
https://example.com to https://www.example.com
http://example.com/index.html to https://www.example.com
http://www.example.com/index.html to https://www.example.com
https://example.com/index.html to https://www.example.com

一切都必须重定向到https://www.example.com,但目前它不起作用。我的错误在哪里,如何改进代码?

我的代码:

    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP:X-Forwarded-SSL} !on
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^/?$ "https\:\/\/www\.example\.com\/" [R=301,L]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
    RewriteRule . http://www.%{HTTP_HOST}%1 [R=301,NE,L]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule . http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
    RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
    RewriteRule . %1 [R=301,NE,L]

可能重复

也许我不明白,但对我来说 htaccess redirect to https://www 没有回答我的问题,因为它没有解决我问题的 /index.html 部分。

【问题讨论】:

标签: .htaccess redirect mod-rewrite https


【解决方案1】:

将 .htaccess 文件中的重写更改为以下规则:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^\/index.(htm|html)$
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ https://www.example.com/ [L,R=301]

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]

【讨论】:

    猜你喜欢
    • 2021-11-10
    • 2012-12-23
    • 2016-09-01
    • 2018-07-03
    • 1970-01-01
    • 2022-01-11
    • 2019-12-04
    相关资源
    最近更新 更多