NNoble VPS
← All docs

Web panels

Force HTTPS with .htaccess

Redirect Apache HTTP requests to HTTPS after the certificate is in place.

This only works on Apache with mod_rewrite. Nginx uses a server block instead. Get a certificate working first or you will lock yourself out of HTTP with a broken TLS name.

Rewrite rule

In the site document root .htaccess:

Apache
RewriteEngine On
RewriteCond %${HTTPS} !=on
RewriteRule ^ https://%${HTTP_HOST}%${REQUEST_URI} [R=301,L]

Confirm

Visit http://your-domain and watch it land on https://. Check a second path so you did not only redirect the homepage.

Alternatives

Prefer a redirect in the virtual host if you control Apache config. Reverse proxies should terminate TLS and send X-Forwarded-Proto so the app does not redirect in a loop.