Jump to content

How To Force HTTPS Using .Htaccess


Nathan

Recommended Posts

  • Administrators

I know this is a pretty basic thing, but I've seen so many tutorials out there that are non-dynamic solutions.

Normally when you google "How to force HTTPS using an .htaccess file" or "Redirect HTTP to HTTPS using an .htaccess file" you get examples like the below.

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

This does work, but you have to replace www.example.com with your domain and it's then hard-coded and not dynamic.  I don't like hard-coding anything unless it's absolutely necessary, so here's an example of the dynamic way to redirect to https from http requests using your htaccess file.

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

View the Prodjex Blog post on How To Force HTTPS Using .Htaccess.

Link to comment
Share on other sites

  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...