Skip to content

Instantly share code, notes, and snippets.

@vadirajks
Last active May 19, 2022 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vadirajks/ffd6e742fbf66b010820eb379d4978e4 to your computer and use it in GitHub Desktop.
Save vadirajks/ffd6e742fbf66b010820eb379d4978e4 to your computer and use it in GitHub Desktop.
http to https redirect
server {
# all traffic, secure or otherwise, comes in on 80 from the ELB
listen 80;
# ELB stores the protocol used between the client and the load balancer in the X-Forwarded-Proto request header. Check for 'https' and redirect if not
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
server_name your-secure-site.com www.your-secure-site.com;
.... (the rest of your config)
}
server {
server_name example.com www.example.com;
listen 80;
listen 443 ssl; # Listen for SSL at port 443 as well
# ... other config - certificates and such
# If a user tries to come through http, redirect them through https
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
<VirtualHost *:80>
...
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI}
...
## option 1
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L]
## option 2
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
## option 3
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
################################################################
## option 4
## https://toster.ru/q/174797
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
################################################################
## option 5
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=302,L]
################################################################
## option 6
# BEGIN rlrssslReallySimpleSSL rsssl_version[2.3.14]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment