简体   繁体   中英

How to redirect http://domain.com to https://www.domain.com?

I already got an SSL certificate. I got Wordpress installed see folder structure in my hostgator file manager below:

I already put define('FORCE_SSL_ADMIN', true); in wp_config.php

Wordpress General settings: WordPress Address (URL): https://www.domain.com/blog Site Address (URL): https://www.domain.com

public_html
  -> blog (inside is the wordpress installation)

database structure
  -> wp_options
      -> siteurl: https://www.domain.com/blog
      -> home: https://www.domain.com

I tried hostgator cpanel redirects, but not works.

I also tried some of the solutions editing .htaccess while searching in google.

Maybe I missed on something regarding with the folder structure.

.htaccess :

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /blog/

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

# END WordPress

You typically just add the rules to your wordpress rules. They go before wp rules.

RewriteEngine On
#rewrite http to https and add www. All cases covered
RewriteCond %{HTTPS} !^on [OR]
RewriteRule %{HTTP_HOST} ^exmaple\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

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

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

# END WordPress

You will need 2 virtual hosts, one for port 443 (https) and one for port 80. In the port 80 you could simply setup a redirect rule for all traffic

  <VirtualHost *:80>
  ...
    RewriteEngine On
    RewriteRule .* https://www.domain.com [R=301,L]
  ...
  </VirtualHost>

Use

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com [NC] 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

to force SSL on all conditions (with or without the www ).

Also, as you have done, change to https in Wordpress General settings.

Do not add the force SSL rule inside the # BEGIN WordPress and # END WordPress rewrite block, becuase if you ever resave permalinks (or even a plugin does) your SSL rules will be deleted.

And then use the developer tools in Firefox (or Firebug ) or Chrome or Safari or IE to see if you have any non-secure element errors, such as from hardcoded http:// URLs in stylesheets and functions.php files.

This is my last resorts for the fix. I put up a php code in the header.php that checks if the inputted url is https if not then it redirects to the url with https.

<?php
    if (empty($_SERVER['HTTPS'])) {
        header('Location: https://domain.com');
        exit;
    }
?>

Thanks all,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM