如何使用漂亮的URL将HTTP重定向到HTTPS?

时间:2016-01-13 23:53:20

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

我想将用户进入我的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)或带有&#39;在此处找到的页面&#39;在哪里&#39;在这里&#39;是我的域名的锚点:/ 忘了提,我只能编辑.htaccess。

1 个答案:

答案 0 :(得分: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]