使用.htaccess强制HTTPS-卡在重定向循环中

时间:2014-04-13 作者:SkyHiRider

我的Wordpress网站位于示例中。com/wordpress和我需要里面的所有东西都只能使用SSL访问。

我创建了一个。重定向http->https的htaccess规则:

RewriteEngine On
RewriteRule ^/?(.*) https://%{SERVER_NAME}/wordpress/directory/$1 [R,L]
实施此规则后,网站将无法访问,因为它被困在重定向循环中。

我做错了什么?

我的Wordpress。htaccess:

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

5 个回复
SO网友:Ricardo Pimentel

这是一个迟来的答案,但这对我来说很有用:

将重写条件更改为

RewriteCond %{HTTP:X-Forwarded-Proto} !https
应该是这样的:

RewriteEngine On 
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://yourdomain.com/$1 [R,L]

SO网友:Frederik Spang

如果不是htaccess专家,我会说你错过了RewriteCond - 条件。

检查它是否在端口80上。尝试以下操作:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^/?(.*) https://%{SERVER_NAME}/wordpress/directory/$1 [R,L]

UPDATE:

在看到您更新的问题后,使用完整的。htaccess-我会这样做:

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

RewriteCond %{HTTPS} off
RewriteRule ^/?(.*) https://%{SERVER_NAME}/wordpress/directory/$1 [R,L]
</IfModule>
让你的条件和规则符合Wordpress的标准。

SO网友:Abed Putra
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Source https://www.godaddy.com/help/redirect-http-to-https-automatically-8828

SO网友:KJThaDon

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
或者仅仅是一页

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} directory
RewriteRule ^(.*)$ https://www.example.com/directory/$1 [R,L]
或者您可以将此php添加到特定文件

if (!isset($_SERVER[\'HTTPS\']) || !$_SERVER[\'HTTPS\']) { // if request is not secure, redirect to secure url
$url = \'https://\' . $_SERVER[\'HTTP_HOST\']
. $_SERVER[\'REQUEST_URI\'];

header(\'Location: \' . $url);
exit;
}
或者将wordpress重写规则与SSL规则相结合。。

RewriteRule ^(.*)$ https://www.example.com/directory/index\\.php[R,L]

SO网友:krunal sojitra

负载平衡器或反向代理背后支持HTTP_X_FORWARDED_PROTO 可以通过添加following code to the wp-config.php file, 在require\\u once调用上方:

if (isset($_SERVER[\'HTTP_X_FORWARDED_PROTO\']) && $_SERVER[\'HTTP_X_FORWARDED_PROTO\'] == \'https\')
    $_SERVER[\'HTTPS\'] = \'on\';

or by using a plugin

添加到网站上的WordPress插件文件夹中,然后在插件管理中激活它

<? /* Plugin Name: Set headers */
if (stripos(get_option(\'siteurl\'), \'https://\') === 0) {
    $_SERVER[\'HTTPS\'] = \'on\';
    // add JavaScript detection of page protocol, and pray!
    add_action(\'wp_print_scripts\', \'force_ssl_url_scheme_script\');
}
function force_ssl_url_scheme_script() {
    ?>
    <script>
    if (document.location.protocol != "https:") {
        document.location = document.URL.replace(/^http:/i, "https:");
    }
    </script>
    <?php
}
Source

它就像

  • http://www.exmaple.com/blog/ to https://www.exmaple.com/blog/
  • http://www.exmaple.com/ to https://www.exmaple.com/

结束