301 redirects after Lets Encrypt install through Siteground

I have just started to install Lets Encrypt on a couple of my HTTP domain names I have hosted on a shared server at Siteground. Everything seems to work fine with the websites.
I have HTTPS Settings set to ‘HTTPS Enforce = On’ and ‘External Links Rewrite = On’.
Before changing to HTTPS, when my sites were just plain old HTTP I had the following in my .htaccess file (to change all none-www to www e.g. http://domain to http://www.domain):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.co.uk [NC]
RewriteRule ^(.)$ http://www.mydomain.co.uk/$1 [L,R=301]
RewriteCond %{the_request} ^(GET|POST|HEAD)\ /index.php\ HTTP/
RewriteRule ^index.php$ http://www.mydomain.co.uk/ [R=301,L]
RewriteRule ^([0-9]+)_(.
)$ http://www.mydomain.co.uk/certain-page.php?id=$1 [P]

I have no idea what configurations Siteground.com applies to my sites when opting to use ‘Lets Encrypt’ facility to apply HTTPS to my domains but it seems to work at this is great.

Before I go further, as I am a new user to this site I am unable to post proper web links but you should get the Gist of things.

What I would like to know is if I should now remove the re-write code from .htaccess file or just leave it?
Will it affect Google indexing?
I previously had, in my Google Webmaster Tools the following sites added:
http://mydomain and http://www.mydomain but the only site that ever got crawled or had any information about it in my console was the http://www.mydomain - I assume because of the re-write rule above.
Now I have the following currently in my Google Webmaster Tools:

  1. http//mydomain
  2. http//www.mydomain
  3. https//mydomain
  4. https//www.mydomain
    When I run the ‘Fetch as Google’ on each of the 4 domains above I get the following:
    For 1. http//mydomain - this is redirecting to - https//mydomain
    For 2. http//www.mydomain - this is redirecting to - https//www.mydomain
    For 3. https//mydomain - this is redirecting to - http//www.mydomain
    For 4. https//www.mydomain - this renders the page ok
    Number 1. to me seems ok as a none-www http site is redirecting to a none-www https site.
    Number 2. to me seems ok as a www http site is redirecting to a www https site.
    What I don’t understand is why Number 3 above a none-www https site is redirecting to a www http site???
    Doesn’t that mean that Number 3 above is being redirected twice for example:
    https//mydomain - redirecting to - http//www.mydomain - 2nd redirect to https//www.mydomain (as in No’s 3 then 2 above)
    Is it possible to just have them all redirect to https//www.mydomain?

Sorry for the long winded approach but only way I can describe it.
Thanks in advance for help.

The short answer: YES.

The long answer... You will need to figure out where the redirections are being done before you can make such a change (which may require SiteGround support and/or good web searches).

Hello,

Hristo from SiteGround here. The Let’s Encrypt Tool and it’s “Force HTTPS” option is designed for convenience. It uses a specially built module for our web server that dynamically rewrites URLs. It’s not using your rules in the .htaccess. It is designed to fix your site if it’s trying to insecurely load any resources.

Then, you have the .htaccess rules which do basically the same. However, .htaccess files do things like forcing www. or stripping it, rewrites and so on. Some applications like WordPress rely on it to construct links. Since you want to strip WWW, I would use the followin .htaccess code which handles all the cases.

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

Feel free to contact our suport team if you need any additional assistance :slight_smile:

2 Likes

Hi there Pandjarov/Hristo,
Firstly may I say a big thank you for responding to my query so promptly it is very much appreciated.
Secondly apologies if you misunderstood my original query!
Basically when my websites were plain old HTTP I wanted to force WWW to be used on all URL’s
Now I have switched to HTTPS I still want to do the same and force HTTPS://WWW on all URL’s
My biggest concern was within Google Webmaster Tools.
It advises to add all connotations of a website in GWT:
HTTP & HTTP://WWW & HTTPS & HTTPS://WWW
Webmaster Tools is currently indexing my HTTP://WWW.DOMAIN is it just a waiting game now where I must wait for google to re-index the https://www URL’s ?
Do I need to add the code you have provided above to my .htaccess file to inform Google that I wish to force HTTPS://WWW to be used and the only one indexed?
Should I be informing Google that the other 3 versions are duplicates and placing some kind of canonical link in the head e.g link rel=“canonical” href=“https://www.example.com
If these were just simple web pages I understand what is duplicate and what isn’t and where a canonical link should go but I am slightly baffled to say the least when it comes to http and https and none-www and www, I am trying my best to learn though.
Thanks in advance.

Should I be putting an exclamation mark ! in front of both the ^ in the rewrite conditions lines 3 and 4 of the code you provided to force www ?
Many Thanks.

Google webmasters tools asks for you to register both www and non-www version of your domain so you can chose preferred domain name for that property. Check out the settings and select whether you want your domain to be indexed with or without www. Then, make the appropriate redirect. As for the HTTPS - don’t worry about that, simply edit the property and change http to https and let the redirect do its magic :slight_smile:

My .htaccess forces both www and https. Not sure about what functionality you’re after with your lines though.

Hi Hristo, thanks for coming to help out with this!

1 Like

Thank you Hristo I wasn’t sure that your .htaccess forced WWW as you mentioned in your first comment “Since you want to strip WWW” so I presumed your code was stripping the WWW. And of course I want to force WWW. If the .htaccess code you supplied does indeed force WWW then I will put it into mine.
Thank you once again for taking the time to reply and help me understand things better. Cheers!

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.