Redirect HTTP traffic to HTTPS
Send every insecure request to the secure version with a permanent 301 redirect.
10 min
why this matters
Old links, printed URLs and typed addresses still start at http://. If they stay there, visitors see a security warning and search engines see two copies of every page.
Step by step
- 1
Add the redirect at your web server
Return a 301 for every HTTP request rather than serving content on port 80.
# nginx server { listen 80; server_name example.com www.example.com; return 301 https://example.com$request_uri; } - 2
Apache alternative
If you run Apache with .htaccess, use mod_rewrite instead.
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://example.com/$1 [R=301,L] - 3
Add HSTS once you are confident
HSTS tells browsers never to try HTTP again, which removes the redirect round trip. Only add it after HTTPS has worked without issue for a while.
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
how to check it worked
- curl -I http://example.com returns 301 with an https:// Location
- No page on the site is reachable over plain HTTP
related guides
Fix a site that isn't responding
None of the four common addresses for your domain returned a working page. Work through DNS, then the server, then the firewall.
Put your site on HTTPS
Install a free TLS certificate and serve every page over HTTPS.
Pick one canonical hostname
Serve your site from a single address and redirect all the others to it.
Cut your server response time
Get time-to-first-byte under 500ms with caching and a CDN.