WordPress通过.htaccess将所有HTTP请求重定向到HTTPS

时间:2016-08-22 作者:Michael Cropper

我以前做过很多次,但不知什么原因,我不会再这样做了。尝试了无休止的解决方案。我所尝试的一切都会导致无休止的重定向循环。

这样做的最佳选择是什么?

Update 1这里有一些尝试过的东西;

RewriteCond %{HTTP_HOST} ^website\\.co\\.uk [NC]
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.website.co.uk/$1 [R,L]
和;

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.website.co.uk$1 [R,L]
在不同的浏览器中测试以防止缓存问题。还是没什么,只是得到了持续的重定向循环。

9 个回复
最合适的回答,由SO网友:Ethan O\'Sullivan 整理而成

我明白了,当您输入指向主页以外页面的链接时,例如:

  • http://www.michaelcropper.co.uk/contact-me
  • www.michaelcropper.co.uk/contact-me
  • michaelcropper.co.uk/contact-me
如果https:// 不在前缀中,而是加载HTTP链接。将以下内容添加到.htaccess<IfModule mod_rewrite.c> 标签:

RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
如果没有对您的.htaccess, 它应该如下所示:

# 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]
# Rewrite HTTP to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
</IfModule>
# END WordPress
让我知道进展如何。

SO网友:Siddharth Shukla

在中添加代码。htaccess文件

 <IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /

   RewriteCond %{HTTPS} !=on
   RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

SO网友:Iggy

您可以在中设置标题。htaccess

<IfModule mod_headers.c>
    Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=HTTPS
</IfModule>

SO网友:Jasom Dotnet

在你的.htaccess:

RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</IfModule>
# END WordPress

SO网友:zihadul

只需将此添加到您的。htaccess文件

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

SO网友:Chris Sprague

如果您使用的是W3 Total Cache,@Ethan Jinks O\'Sullivan的答案只有在您将重定向放在W3 Total Cache mods之前时才有效。htaccess如下所示

#BEGIN Rewrite HTTP to HTTPS
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
</IfModule>
#END Rewrite HTTP to HTTPS

# BEGIN GZIP COMPRESSION
...
# END GZIP COMPRESSION

# BEGIN DEFLATE COMPRESSION
...
# END DEFLATE COMPRESSION

# BEGIN W3TC Browser Cache
...
# END W3TC Browser Cache

# BEGIN W3TC Page Cache core
...
# END W3TC Page Cache core

# 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

SO网友:vin decode

如果您不想在上更改任何内容。htaccess您可以尝试一下

add_action(\'template_redirect\', \'redirect_core\', 50);
add_action(\'init\', \'redirect_core\', 50);
add_action(\'wp_loaded\', \'redirect_core\', 50);
function redirect_core(){
  if (!is_ssl()) {
    wp_redirect(\'https://\' . $_SERVER[\'HTTP_HOST\'] . $_SERVER[\'REQUEST_URI\'], 301);
    exit();
  }
}
或者尝试此插件:HTTPS Redirect

SO网友:user3469142

给我10分钟来解决这个问题

为您的域添加conf文件。

转到centos 7/etc/httpd/conf.d/, 然后创建一个名为anything.conf.

在其上添加以下代码:

<VirtualHost *:80>
    ServerName domain.com
    ServerAlias www.domain.com
    ServerAdmin [email protected]
    DocumentRoot /var/www/html

    <Directory /var/www/html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog /var/log/httpd/example.com-error.log
    CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
更换example.com 使用您的域
然后在上添加https重写代码。htaccess its将正常工作。:)

SO网友:vvvvv
#BEGIN Rewrite HTTP to HTTPS
<IfModule mod_rewrite.c>
  RewriteCond %{HTTPS} !=on
  RewriteRule ^(.*) https://%{SERVER_NAME}/$1 [R,L]
</IfModule>
#END Rewrite HTTP to HTTPS

# 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

相关推荐

Redirect Css files to https

在哪里可以找到负责调用Css文件的文件,并测试它们的链接是http还是https?我想测试请求是否为https,如果是,那么很好,如果不是,请将其更改为https在函数中。php我找到了负责调用主题Css的函数,下面是它的代码:if (!function_exists(\'jobcareer_front_scripts\')) { function jobcareer_front_scripts() { global $jobcareer_options;