
Hello there. Today I wanted to answer a question that just came in, while it's still fresh. I had previously shared how to redirect to an SSL address using PHP. But that method was for custom-built PHP projects (i.e. not WordPress, Joomla, Drupal, etc.).
So what if you're using a ready-made script or CMS? The answer is simple — you'll use your .htaccess
file.
There are plenty of examples for this online, but here’s a commonly used one:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Let me explain for the curious. First, we enable Apache RewriteEngine. Then, we check whether the URL is not using HTTPS — if not, we redirect it to the HTTPS version. After that, we check if the domain includes www. If it doesn’t, we redirect it to the https://www. version. That’s it!
Of course, this involves a couple of redirect conditions. If you want to simplify it even more, you can use this instead:
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
That should do the trick. If you have any questions, feel free to leave a comment below.
Related Articles

Installing Apache Tomcat Server on Ubuntu
