【问题标题】:Cloudflare flexible SSL and redirectsCloudflare 灵活的 SSL 和重定向
【发布时间】:2014-10-09 01:22:21
【问题描述】:

我的域遇到了一些非常烦人的问题。

我的网站上有 Cloudflare 灵活 SSL,如果我自己键入它,它会进行 https 调用 >.htaccess 文件强制在我的网站上使用 https 时,我收到内部服务器错误。

我希望我网站的所有访问者始终被重定向到 https://www.example.com,即使他们在整个网站上使用 https 键入 example.comexample.com/index.php 等。

我只是找不到解决方案,所以我正在尝试堆栈。

我在 Cloudflare 上的 DNS 设置是一条将 domain.com 指向 IP 的 A 记录 并且一条 CNAME 记录说 www.domain.comdomain.com 的别名

我没有在 Cloudflare atm 上添加页面规则。

    <IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
        <FilesMatch "\.(cur|gif|ico|jpe?g|png|svgz?|webp)$">
            SetEnvIf Origin ":" IS_CORS
            Header set Access-Control-Allow-Origin "*" env=IS_CORS
        </FilesMatch>
    </IfModule>
</IfModule>

<IfModule mod_headers.c>
    <FilesMatch "\.(eot|otf|tt[cf]|woff)$">
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>
</IfModule>


Options -MultiViews


<IfModule mod_headers.c>
    Header set X-UA-Compatible "IE=edge"
    # `mod_headers` cannot match based on the content-type, however, this
    # header should be send only for HTML pages and not for the other resources
    <FilesMatch "\.(appcache|atom|crx|css|cur|eot|f4[abpv]|flv|gif|htc|ico|jpe?g|js|json(ld)?|m4[av]|manifest|map|mp4|oex|og[agv]|opus|otf|pdf|png|rdf|rss|safariextz|svgz?|swf|tt[cf]|vcf|vtt|webapp|web[mp]|woff|xml|xpi)$">
        Header unset X-UA-Compatible
    </FilesMatch>
</IfModule>

<IfModule mod_mime.c>

  # Audio
    AddType audio/mp4                                   m4a f4a f4b
    AddType audio/ogg                                   oga ogg opus

  # Data interchange
    AddType application/json                            json map
    AddType application/ld+json                         jsonld

    AddType application/javascript                      js

  # Video
    AddType video/mp4                                   f4v f4p m4v mp4
    AddType video/ogg                                   ogv
    AddType video/webm                                  webm
    AddType video/x-flv                                 flv

  # Web fonts
    AddType application/font-woff                       woff
    AddType application/vnd.ms-fontobject               eot


    AddType application/x-font-ttf                      ttc ttf
    AddType font/opentype                               otf


    AddType     image/svg+xml                           svgz
    AddEncoding gzip                                    svgz

  # Other
    AddType application/octet-stream                    safariextz
    AddType application/x-chrome-extension              crx
    AddType application/x-opera-extension               oex
    AddType application/x-web-app-manifest+json         webapp
    AddType application/x-xpinstall                     xpi
    AddType application/xml                             atom rdf rss xml
    AddType image/webp                                  webp
    AddType image/x-icon                                cur
    AddType text/cache-manifest                         appcache manifest
    AddType text/vtt                                    vtt
    AddType text/x-component                            htc
    AddType text/x-vcard                                vcf

</IfModule>

AddDefaultCharset utf-8


<IfModule mod_mime.c>
    AddCharset utf-8 .atom .css .js .json .jsonld .rss .vtt .webapp .xml
</IfModule>


#<IfModule mod_rewrite.c>
#     Options +FollowSymlinks
   # Options +SymLinksIfOwnerMatch
#    RewriteEngine On
   # REDIRECT /folder/index.php to /folder/
#    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
#    RewriteRule ^(([^/]+/)*)index\.php$ http://www.domain.dk/$1 [R=301,L]
#</IfModule>


#<IfModule mod_rewrite.c>
#    RewriteCond %{HTTPS} !=on
#    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
#    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
#</IfModule>

#<IfModule mod_rewrite.c>
#    RewriteCond %{HTTPS} !=on
#    RewriteCond %{HTTP_HOST} !^www\. [NC]
#    RewriteCond %{SERVER_ADDR} !=127.0.0.1
#    RewriteCond %{SERVER_ADDR} !=::1
#    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#</IfModule>


<IfModule mod_autoindex.c>
    Options -Indexes
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule "(^|/)\." - [F]
</IfModule>

<FilesMatch "(^#.*#|\.(bak|config|dist|fla|in[ci]|log|psd|sh|sql|sw[op])|~)$">

    # Apache < 2.3
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
        Satisfy All
    </IfModule>

    # Apache ≥ 2.3
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>

</FilesMatch>

<IfModule mod_headers.c>
     Header set X-Content-Type-Options "nosniff"
</IfModule>

#<IfModule mod_rewrite.c>
#    RewriteCond %{SERVER_PORT} !^443
#    RewriteRule ^ https://www.domain.dk%{REQUEST_URI} [R=301,L]
#</IfModule>



<IfModule mod_deflate.c>

    # Force compression for mangled headers.
    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
            RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
        </IfModule>
    </IfModule>

    # Compress all output labeled with one of the following MIME-types
    # (for Apache versions below 2.3.7, you don't need to enable `mod_filter`
    #  and can remove the `<IfModule mod_filter.c>` and `</IfModule>` lines
    #  as `AddOutputFilterByType` is still in the core directives).
    <IfModule mod_filter.c>
        AddOutputFilterByType DEFLATE application/atom+xml \
                                      application/javascript \
                                      application/json \
                                      application/ld+json \
                                      application/rss+xml \
                                      application/vnd.ms-fontobject \
                                      application/x-font-ttf \
                                      application/x-web-app-manifest+json \
                                      application/xhtml+xml \
                                      application/xml \
                                      font/opentype \
                                      image/svg+xml \
                                      image/x-icon \
                                      text/css \
                                      text/html \
                                      text/plain \
                                      text/x-component \
                                      text/xml
    </IfModule>

</IfModule>


<IfModule mod_headers.c>
   Header set Cache-Control "no-transform"
</IfModule>

<IfModule mod_headers.c>
    Header unset ETag
</IfModule>

FileETag None

#<IfModule mod_expires.c>

#ExpiresActive On
#ExpiresByType image/jpg "access 1 year"
#ExpiresByType image/jpeg "access 1 year"
#ExpiresByType image/gif "access 1 year"
#ExpiresByType image/png "access 1 year"
#ExpiresByType text/css "access 1 month"
#ExpiresByType text/html "access 1 month"
#ExpiresByType application/pdf "access 1 month"
#ExpiresByType text/x-javascript "access 1 month"
#ExpiresByType application/x-shockwave-flash "access 1 month"
#ExpiresByType image/x-icon "access 1 year"
#ExpiresDefault "access 1 month"

#</IfModule>

【问题讨论】:

  • 错误日志中出现了哪些错误?如果您收到 500 错误,则应将其记录下来。
  • 我正在我的 htaccess 文件中尝试这个。 RewriteCond %{HTTPS} !^=on$ RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{SERVER_ADDR} !=127.0.0.1 RewriteCond %{SERVER_ADDR} !=::1 RewriteRule ^ %{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  • 你能用你的htaccess编辑你的问题吗?另外,您能否告知是否已启用 cloudflare 规则来处理静态 HTML,或者您是否尚未配置它们。因为这将告知我们 cloudflare 是否正在处理整个网站本身。但是,它看起来确实像 htaccess 文件中的错误配置。因此,您需要发布出现在错误日志文件中的错误(通常在 /var/log/apache( 或 httpd)/error.log 或 error_log
  • 我对这一切不太感兴趣,我对 apache、htaccess 等还是很陌生。但是发生了一些奇怪的事情。我试图用简单的 etag removel 制作一个 htaccess 文件。然后我得到内部服务器错误。此外,如果文档为空。我将使用原始 htaccess 编辑我的 Q,并且该站点没有应用 cloudflare 规则。我刚刚激活了 cloudflare 来处理所有流量。

标签: .htaccess redirect ssl cloudflare


【解决方案1】:

来自 CloudFlare 的知识库。

RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://www.domain.com$1 [L]

https://support.cloudflare.com/hc/en-us/articles/200170536-How-do-I-redirect-HTTPS-traffic-with-Flexible-SSL-and-Apache-

这是我自己做的,效果很好。

【讨论】:

  • 只使用这个 wordpress 工作。如果我尝试打开除 wordpress 文件之外的任何其他文件,它们仍会显示重定向。像 / 是 wordpress 的基本 URL /myfolder/ 我自己创建了一个文件夹并放置了一些 png 文件并打开文件总是显示重定向。如果我打开开发模式它可以工作。但是当开发模式关闭时。它不会工作。我已经尝试清除所有内容的缓存。最后离开 Cloudflare SSL。它充满了错误!
【解决方案2】:

如此处所述在Cloudflare Official Support Site

所以我会引导你完成我所做的,(截至 2016 年 6 月 16 日

因为一张图片能说出一千个单词。见下文

你就完成了。它应该开始直接重定向到 https。

【讨论】:

    【解决方案3】:

    尝试以下重写规则:

    <IfModule mod_rewrite.c>
     RewriteCond %{HTTPS} off
     RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
     RewriteCond %{HTTP_HOST} !^www\.
     RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    </IfModule>
    

    另外请检查您的 httpd.conf/apache.conf 文件中是否启用了虚拟目录的 SSL 版本。

    您是如何管理域名的?通过控制面板还是通过命令行?您使用的是哪一类云耀斑?您是否在服务器上安装了自己的 SSL 证书?

    【讨论】:

    • 它不起作用。我在我的 apache2 文件夹中找不到有关虚拟目录的任何信息。我现在不使用命令行(仍然是初学者)。我不使用任何类别的 cloudflare?我刚刚将我的 DNS 指向他们的,并为我的专业帐户激活了 SSL flex。我没有自己的 SSL。
    • 对不起,我的意思是上课,你是否使用免费的 cloudflare,商业等。此外,错误可能是当你强制 https 时它没有被 cloudflare 处理
    • 我使用 pro :) 他们说我需要制作一个自签名 SSL 并将其上传到 unoeuro(我的主机),然后我可以使用完整的 SSL 而不是 flex SSL。看来,flex SSL 可能会导致我的服务器和 cloudflare 出现许多重定向问题。
    • 它将是什么类型的应用程序?商业网站?如果是这样,它将无法正常工作。它同样安全,但由不受信任的站点签名,因此每个人都会收到 SSL 警告(没有人会去)
    • 我认为 https 不是主要的排名因素。但是,如果您想使用 ssl,我建议您购买 ssl 证书
    【解决方案4】:

    .htaccess 对我也不起作用(也没有使用强制 https 选项创建规则)。 有效的是我在 CloudFlare 上创建了一个重写规则,将 http://yourdomain.com/* 重写为 https://yourdomain.com/$1 并解决了问题(尽管花了大约半个小时才开始)。

    【讨论】:

      【解决方案5】:

      这对我有用: @Victor Häggqvist 的 htaccess 规则与 @Mohd Abdul Mujib 的页面规则相结合(不过,* 也在规则的开头)。

      RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
      RewriteRule ^(.*)$ https://www.domain.com$1 [L]
      ------
      http://*yourdomain.com/*
      

      根据 Cloudflare 的手册,上面的协议必须是 http 才能工作。

      我向 Cloudflare 添加了一个纯 html 页面并获得了太多重​​定向,但上述方法有所帮助。现在我要去网站上安装 Wordpress。

      这些是我为我的旅程收集的链接,以防它们对其他人也有帮助。

      https://support.cloudflare.com/hc/en-us/articles/200170536-How-do-I-redirect-HTTPS-traffic-with-Flexible-SSL-and-Apache-

      https://blog.cloudflare.com/flexible-ssl-wordpress-fixing-mixed-content-errors/

      https://support.cloudflare.com/hc/en-us/articles/201717894-Using-CloudFlare-and-WordPress-Five-Easy-First-Steps

      https://support.cloudflare.com/hc/en-us/articles/203487280--How-do-I-fix-the-infinite-redirect-loop-error-after-enabling-Flexible-SSL-with-WordPress-

      【讨论】:

        猜你喜欢
        • 2015-05-27
        • 2016-03-24
        • 2017-09-24
        • 1970-01-01
        • 2014-12-02
        • 2016-08-10
        • 1970-01-01
        • 2016-04-24
        • 1970-01-01
        相关资源
        最近更新 更多