用于https的RewriteRule不起作用

时间:2017-01-25 20:51:09

标签: apache .htaccess https

我试图让我的网站加载https版本,无论用户来自何处或输入什么内容(例如:none example.com)。

我的htaccess文件中有以下代码,但如果我只输入example.com,则不会将网站加载为https://example.com。我已经清除了缓存并将cloudflare转换为开发人员模式。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) https://%1/$1 [R=301,NE,L]
RewriteCond %{HTTPS} off

之前我也有这一行,但它引起了重定向循环。

RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

任何人都能看到我做错了什么?

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]


ErrorDocument 404 /404custom.php

<ifmodule deflate_module.c>
    DeflateCompressionLevel 1
    DeflateBufferSize 8096
    DeflateMemLevel 8
    DeflateWindowSize 8

    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/xml
</ifmodule>

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/gif A31536000
    ExpiresByType image/jpg A31536000
    ExpiresByType image/jpeg A31536000
    ExpiresByType image/png A31536000
    ExpiresByType image/bmp A31536000
    ExpiresByType image/ico A31536000
    ExpiresByType image/ico A31536000
    ExpiresByType image/icon A31536000
    ExpiresByType image/x-icon A31536000
    ExpiresByType text/css A31536000
    ExpiresByType text/javascript A31536000
    ExpiresByType application/javascript A31536000
    ExpiresByType application/x-javascript A31536000

    # Add a far future Expires header for fonts
    ExpiresByType application/vnd.ms-fontobject A31536000
    ExpiresByType application/x-font-ttf A31536000
    ExpiresByType application/x-font-opentype A31536000
    ExpiresByType application/x-font-woff A31536000
    ExpiresByType image/svg+xml A31536000

    <FilesMatch "\.(js|css|xml|gz)$">
        Header append Vary: Accept-Encoding
    </FilesMatch>

</IfModule>

RedirectPermanent /test_index  https://example.com

1 个答案:

答案 0 :(得分:1)

您的孤儿RewriteCond无效。你还需要这样的OR子句:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]

清除浏览器缓存后进行测试。