【问题标题】:WordPress Enforce https through .htaccessWordPress 通过 .htaccess 强制执行 https
【发布时间】:2018-10-04 12:51:30
【问题描述】:

由于 Google 已缓存/保存旧链接,我一直在进行一些 301 重定向。所以我设置了我的 301 重定向:

Redirect 301 /old/url/ /index.php/old/url

但是我注意到网址不是https://,而是www.example.com/index.php/old/url

我通过.htaccess 在线搜索了强制https,但我尝试的一切都会导致整个网站出现错误

页面未正确重定向

这是我的 .htaccess(减去 301 重定向):

# 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

# force https
# RewriteCond %{HTTPS} !=on
# RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# RewriteCond %{HTTPS} !=on
# RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

# ensure https
# RewriteCond %{HTTP:X-Forwarded-Proto} !https 
# RewriteCond %{HTTPS} off
# RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

如您所见,我尝试了几种不同的方法来实现这一点,但所有都产生了上述错误(它们并非同时都未注释)。

我的网站都是 https,除了当你搜索它们时谷歌有这个旧链接,但重定向不包括 https 部分,如果我尝试做一个完整的 url(例如Redirect 301 /old/url/ https://www.example.com/old/url)那么它还是不行。

如何在不破坏网站的情况下为 www.example url 强制执行 https?

谢谢

【问题讨论】:

    标签: wordpress apache .htaccess redirect


    【解决方案1】:

    首先,您需要访问设置 » 常规页面。从这里您需要通过将 http 替换为 https 来更新您的 WordPress 和站点 URL 地址字段。

    您需要通过将以下代码添加到您的 .htaccess 文件来设置 WordPress 从 HTTP 到 HTTPS 的重定向。

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

    如果您想在您的 WordPress 管理区域或登录页面上强制使用 SSL 和 HTTPS,那么您需要在 wp-config.php 文件中配置 SSL。在“就是这样,停止编辑!”上方添加以下代码wp-config.php 文件中的一行:

    define('FORCE_SSL_ADMIN', true);
    

    【讨论】:

    • 这已经到位 -> 抱歉 :/ - 不是 force_ssl_admin 用于管理窗格;?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2013-01-31
    • 2018-05-04
    • 1970-01-01
    • 2012-06-17
    相关资源
    最近更新 更多