1

Force "https" and "www" redirect

I am trying to see if there is a way to redirect url to https://www.example.com For example, if people are entering the url http://example.com/subfolder, then it should redirect to https://www.example.com/subfolder.

This is a Wordpress website. If I go to Settings => General => I see WordPress Address(URL) and Site Address (URL) are grayed out so I can't enter any url. Both shows as http://example.com

I went to phpMyAdmin=> option table to change the WordPress Address (URL) and Site Address (URL) to https://www.example.com but I still see the http://example.com on my wordpress general setting panel.

I looked at the wp-config file and found the following code that appears to be preventing me from entering custom url.

define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST']);

define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST']);

define('WP_CONTENT_URL', '/wp-content');

define('DOMAIN_CURRENT_SITE', $_SERVER['HTTP_HOST']);

It seems like the WordPress Address(URL) and Site Address(URL) are enforced by the codes here.

My hosting provider recommanded using htaccess to enforce the ssl by putting the code below:

#Force SSL on entire site
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://(YOURDOMAIN)/$1 [R,L]

But this guide doesn't really tell us how to enforce www as well.

I found another guide here .htaccess - how to force "www." in a generic way? that explains how to enforce domain.com to www.domain.com. This one supposedly also check https and implement to the rewrite rule.

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Now, someone else is recommanding the following code below, claiming that the first one above will only work if you want all sub-domains forwarded to www.yourdomain.com. I am just confused which one I need to choose?

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

I am trying to see which option would be the best solution to implement the force redirect? Or should I get rid of those four define() codes from wp-config file so I can setup redirect with wordpress general setting?

Community
  • 1
  • 1
ChrisP777
  • 199
  • 1
  • 2
  • 19
  • Use this solution [http://stackoverflow.com/questions/41904468/migrating-site-to-from-http-to-https-and-ssl/41941016#41941016](http://stackoverflow.com/questions/41904468/migrating-site-to-from-http-to-https-and-ssl/41941016#41941016) – SamWanekeya Jan 30 '17 at 16:52

0 Answers0