Redirect HTTP to HTTPS on nginx/apache

Apache

install&enable mod rewrite & ssl

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

If you use Amazon linux and installed Amazon linux http package(ie httpd24-2.4.27-3.75.amzn1)

then you need to install mod24_ssl instead of mod_ssl

sudo yum install -y mod24_ssl

nginx
in server block;

if ($scheme != “https”) {
return 301 https://$server_name$request_uri;
}
If you want to redirect www to base domain simply replace $server_name with your base domain(example.com)

 

 

Advertisements
Redirect HTTP to HTTPS on nginx/apache

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s