【问题标题】:wordpress http to https redirect loopwordpress http 到 https 重定向循环
【发布时间】:2014-08-26 18:16:58
【问题描述】:

我正在尝试强制我的网站以 HTTPS 加载。我可以在 PHP 中创建适用于 PHP 文件的重定向,但我希望所有请求都强制使用 HTTPS - JS、CSS、图像等。

这是我的 .htaccess 中的内容:

ErrorDocument 401 default

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

我已经尝试了各种方法,通过将重定向放置在不同的位置等,但一切似乎都使它成为一个重定向循环。有什么想法可以解决这个问题吗?

谢谢。

【问题讨论】:

    标签: wordpress apache .htaccess redirect


    【解决方案1】:

    在内部 WP 规则之前保留您的 httphttps 规则:

    ErrorDocument 401 default
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
    
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    

    确保 WP 的 home URLsite URL 在永久链接设置中也有 https

    【讨论】:

    • 一如既往的 htaccess 大师 :)
    • 不需要加R=302。如果没有代码给出 HTTP 响应,则默认为 302(临时移动)
    • 是的,但 R=302 在测试时应该是占位符。测试后应替换为R=301
    【解决方案2】:

    设置对 SEO 更友好的 301 永久重定向。已测试:

    # BEGIN WordPress
    
    <IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Redirect all traffic to https
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
    
    # Wordpress rules 
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    

    【讨论】:

      猜你喜欢
      • 2015-10-10
      • 1970-01-01
      • 2018-01-18
      • 2016-09-01
      • 2015-10-09
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多