{"id":21701,"date":"2017-03-08T07:37:49","date_gmt":"2017-03-08T13:37:49","guid":{"rendered":"https:\/\/www.rosehosting.com\/blog\/?p=21701"},"modified":"2022-06-03T03:42:16","modified_gmt":"2022-06-03T08:42:16","slug":"how-to-redirect-http-traffic-to-https-in-nginx-and-apache","status":"publish","type":"post","link":"https:\/\/www.rosehosting.com\/blog\/how-to-redirect-http-traffic-to-https-in-nginx-and-apache\/","title":{"rendered":"How to Redirect HTTP Traffic to HTTPS in Nginx and Apache"},"content":{"rendered":"
<\/p>\n
We will show you how to redirect HTTP traffic to HTTPS in Nginx<\/a> and Apache<\/a>. You should always use HTTPS instead of HTTP to protect your website, even if it doesn’t handle sensitive communications. The main reasons to use HTTPS are:<\/p>\n To use HTTPS, you’ll need an SSL Certificate<\/a>. You can get a VPS from us<\/a> and we’ll install the certificate for you and properly configure Nginx\/Apache to redirect all traffic to HTTPS, free of charge.<\/p>\n <\/p>\n To automatically redirect all your visitors to the HTTPS version of your site, add the following code in your site If you want to redirect all your visitors to the HTTPS NON-WWW<\/strong> version of your, site use the following code:<\/p>\n and to redirect all your visitors to the HTTPS WWW<\/strong> version of your site, use the following code:<\/p>\n We need to create three server blocks<\/a>, one for HTTP, one for HTTPS NON-WWW and one for HTTPS WWW versions of the site. The first server block will redirect all visitors entering the site via HTTP to HTTPS and the two other blocks will redirect visitors entering the site via WWW to NON-WWW or vice-versa.<\/p>\n To redirect all HTTP and HTTPS NON-WWW traffic to HTTPS WWW<\/strong>, use the following code:<\/p>\n and to redirect all HTTP and HTTPS WWW traffic to HTTPS NON-WWW<\/strong>, use the following code:<\/p>\n Don’t forget to replace ‘yourdomain.com’ with your actual domain name.<\/p>\n Of course, you don\u2019t have to\u00a0Redirect HTTP Traffic to HTTPS in Nginx and Apache if you use one of our VPS Hosting services<\/a>, in which case you can simply ask our expert Linux admins to set this up for you. They are available 24×7 and will take care of your request immediately.<\/p>\n PS<\/span><\/strong>. If you liked this post, on\u00a0How to Redirect HTTP Traffic to HTTPS in Nginx and Apache,\u00a0 please share it with your friends on the social networks using the buttons below or simply leave a reply in the comments section. Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":" We will show you how to redirect HTTP traffic to HTTPS in Nginx and Apache. You should always use HTTPS … <\/p>\n\n
1. Redirect HTTP to HTTPS using Apache mod_rewrite<\/h2>\n
.htaccess file<\/code><\/p>\n
RewriteEngine On\r\n# redirect http to https\r\nRewriteCond %{HTTPS} off\r\nRewriteRule ^(.*)$ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\r\n<\/pre>\n
RewriteEngine On\r\n# redirect all www to https non-www\r\nRewriteCond %{HTTP_HOST} ^www\\.(.*)$ [NC]\r\nRewriteRule ^(.*)$ https:\/\/%1\/$1 [L,R=301]\r\n# redirect http non-www to https non-www\r\nRewriteCond %{HTTPS} off\r\nRewriteRule ^(.*)$ https:\/\/%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\r\n<\/pre>\n
RewriteEngine On\r\n# redirect all non-www to https www\r\nRewriteCond %{HTTP_HOST} !^www\\.(.*)$ [NC]\r\nRewriteRule ^(.*)$ https:\/\/www.%1\/$1 [L,R=301]\r\n# redirect http www to https www\r\nRewriteCond %{HTTPS} off\r\nRewriteRule ^(.*)$ https:\/\/www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]\r\n<\/pre>\n
2. Redirect HTTP to HTTPS with Nginx<\/h2>\n
server {\r\n listen [::]:80;\r\n listen 80;\r\n\r\n server_name yourdomain.com www.yourdomain.com;\r\n\r\n # redirect http to https www\r\n return 301 https:\/\/www.yourdomain.com$request_uri;\r\n}\r\n\r\nserver {\r\n listen [::]:443 ssl http2;\r\n listen 443 ssl http2;\r\n\r\n server_name yourdomain.com;\r\n\r\n # SSL code\r\n\r\n # redirect https non-www to https www\r\n return 301 https:\/\/www.yourdomain.com$request_uri;\r\n}\r\n\r\nserver {\r\n\r\n listen [::]:443 ssl http2;\r\n listen 443 ssl http2;\r\n\r\n server_name www.yourdomain.com;\r\n\r\n # SSL code\r\n # other code\r\n}\r\n<\/pre>\n
server {\r\n listen [::]:80;\r\n listen 80\r\n\r\n server_name yourdomain.com www.yourdomain.com;\r\n\r\n # redirect http to https non-www\r\n return 301 https:\/\/yourdomain.com$request_uri;\r\n}\r\n\r\nserver {\r\n listen [::]:443 ssl http2;\r\n listen 443 ssl http2;\r\n\r\n server_name www.yourdomain.com;\r\n\r\n # SSL code\r\n\r\n # redirect https non-www to https www\r\n return 301 https:\/\/yourdomain.com$request_uri;\r\n}\r\n\r\nserver {\r\n\r\nlisten [::]:443 ssl http2;\r\nlisten 443 ssl http2;\r\n\r\n server_name yourdomain.com;\r\n\r\n # SSL code\r\n # Other code\r\n}\r\n<\/pre>\n
\n