【问题标题】:How to redirect HTTP to HTTPS with pretty urls?如何使用漂亮的 url 将 HTTP 重定向到 HTTPS?
【发布时间】:2016-04-19 04:08:50
【问题描述】:

我想将进入我的 Webfaction 托管 Wordpress 站点的用户从 HTTP 重定向到 HTTPS,同时支持漂亮的 URL。如何做到这一点?我已经尝试了几个解决方案,但它不起作用。 (以下示例)

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#This is first attempt
#RewriteCond %{HTTPS} =off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] #Tested here some flags, no success

#This is second attempt
#RewriteCond %{HTTPS} =off
#RewriteRule ^(.*)$ - [env=ps:https]

#This is third attempt
#RewriteCond %{SERVER_PORT} ^([0-9]+s)$
#RewriteRule ^(.*)$ - [env=ps:https]

#This is fourth try
#RewriteCond %{HTTPS} =off
#SSLOptions +StrictRequire
#SSLRequireSSL
#SSLRequire %{HTTP_HOST} eq "example.com"

#This is fifth attempt
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

# This is Wordpress pretty URLS (Has to stay!)
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

我得到的只是重定向循环(301 或 302)或带有“在这里找到”的页面,其中“这里”是我的域的锚点:/ 忘了说,我只能编辑 .htaccess。

【问题讨论】:

  • 你为什么不使用插件呢?那里足够了。例如ForceSSL
  • @0x4Dark 该插件是我想省略的不必要的代码。

标签: wordpress apache mod-rewrite https friendly-url


【解决方案1】:

我找到了这个答案https://serverfault.com/a/678402/102060,这对我有帮助。 事实证明,Webfaction 使用了某种负载均衡器,它不会通过发送特定的标头来显示自己。 我试过这个百叶窗,效果很好!

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

【讨论】:

    猜你喜欢
    • 2020-08-07
    • 2018-02-13
    • 1970-01-01
    • 2021-03-22
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    相关资源
    最近更新 更多