How to force redirection to HTTPS

S Isaac

Free Member
Mar 2, 2010
348
29
My host says to copy the following into the .htaccess file

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

My current .htaccess file has the following in it

#WWW Redirect START
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
#WWW Redirect END
# 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
AddType x-httpd-php71 .php

I'm guessing the start of the current file is messing things up? But I know messing with the .htaccess file can screw things up.

Any idea what should and shouldn't be in there?
 
The newly provided lines for the HTTPS redirection are fine.

The lines wrapped in "#WWW Redirect START" are just to redirect to the www version of your website. Those are fine but handle HTTP/HTTPS separately, since you're only using HTTPS you can simplify that section to the following:

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

All in all, I would use the following content:

Code:
RewriteEngine On

# Force HTTPS connections
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Force the www. version
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

# 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

# Set PHP version to 7.1
AddType x-httpd-php71 .php

That said, the rules as they are shouldn't cause an issue. What problem are you running into?
 
  • Like
Reactions: S Isaac
Upvote 0
Also, when I check my site with gtmetrix my https site is 1 second slower. Is that normal?

Are you entering the "final destination" address into gtmetrix, e.g. https://www.yoursite.com, or something else such as "http://yoursite.com"? I ask because the redirections to HTTPS and the www. version can slow down the initial load. This of course is not an issue on subsequent loads once the user is browsing on the correct protocol and hostname.

HTTPS can slow down the initial connection but after the first connection in a single browsing session this should be much less of an issue, so your users are less likely to notice. HTTPS can also help speed up your website in other ways if your hosting provider has deployed HTTP/2 support, as requests for assets such as images can be sent over the same TCP connection as used for the original web page request.
 
  • Like
Reactions: S Isaac
Upvote 0
It's not forcing a redirect to HTTPS.

Are you on Tsohost or one of their other brands, on their cloud platform?

If so please try using the following at the top of your .htaccess file instead:

Code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
 
  • Like
Reactions: S Isaac
Upvote 0
Never mind, I was being a complete muppet and hadn't saved the redirect code. I'd saved it as a text file to desktop instead of using the edit facility in cpanel !!
 
  • Like
Reactions: arnydnxluk
Upvote 0
If the .htaccess redirect is working it should take milliseconds. If what I'm reading, it is taking 0.9 seconds, then that is because WordPress is itself doing the redirect ( settings / general ) which reaqired WP to load and access the database.
 
Upvote 0
A good host would have just done that for you! Especially if you are asking about .htaccess . If you don't know what you are doing with that file I advise not touching it at all. You can easily take down your site with an error or worse break urls and the site without noticing. SEO can be affected.
The redirect in your case given seems fine to me but that file could use a clean up IMO.
 
Upvote 0
Make sure that you're redirect is to your 'Home URL' that is set to in WordPress, if not you're probably seeing a WordPress redirect happening that's giving you the delay.
 
Upvote 0
The OP code rewrite will apply to all url's matching www so you better also check that you are using www. That is personal pref. I always get rid of www google don't care but just make sure that
your url's don't both work. Pick one or the other and stick to it!

The original code will not redirect non www url's as there is no www to match!

I had to remove my examples as I don't have enough credit to show links (even examples) at this time.
 
Upvote 0

Latest Articles