Blog

Hier geht es um Webentwicklung und Nagios Monitoring.

Redirect HTTP to HTTPS using Apache and htaccess

Geschrieben am von Klement

If you're having a SSL certificate for your website and want to redirect the visitors from HTTP to HTTPS for all pages you can do this by adding three simple rules via htaccess (mod_rewrite). You may already have a .htaccess file in the root folder of your website with some other rules. Then you can just edit this file and add the ReqriteCond and RewriteRule. Otherwise create a new file called .htaccess, add the fallowing lines to the file and upload it to the root folder of your website.

#Activate rewrite engine
RewriteEngine On

#Check if visitor is already coming via HTTPS. If not process to the Rewrite Rule
RewriteCond %{HTTPS} off

#Redirects all requests to HTTPS
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

(.*)
Matches all requests and rewrites them

%{HTTP_HOST}
Variable for your domain like www.yoursite.com (more info to find here)

%{REQUEST_URI}
Variable for your path like /subsite/news.html (more info to find here)

R=301
HTTP status code 301 (permanent redirect) will be used for the redirect. This is the best way for search engines when they update your links to HTTPS without any loss (more info to find here).

L
No further rules will be processed if the rule matches (more info to find here).