APACHE

Enforcing HTTPS Redirect with Apache .htaccess

Secure your website by enforcing HTTPS with an Apache .htaccess redirect, automatically sending all HTTP traffic to its encrypted HTTPS equivalent.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
How it works: This Apache `.htaccess` snippet ensures that all HTTP requests to your website are automatically redirected to their HTTPS counterparts using a 301 (permanent) redirect. It first checks if the `mod_rewrite` module is enabled, which is necessary for URL rewriting. Then, `RewriteEngine On` activates the rewrite engine. `RewriteCond %{HTTPS} off` checks if the current connection is *not* HTTPS. If it's HTTP, `RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]` redirects the request to the same URL but with the `https://` schema. This is crucial for protecting data in transit, maintaining data integrity, and improving SEO.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs