Apache and nginx: Redirect to HTTPS with a 301 Redirect

By | 2014/10/09

Instead of using rewrite rules, consider using a sweet 301 redirect to force HTTPS. Check it out!


By using a 301 redirect there will be better performance than having the server process any rewrite rules.


Apache example which can be in the port 80 virtualhost config. This will redirect to your HTTPS port 443 virtualhost config:

<VirtualHost *:80>
...

        Redirect 301 / https://mydomain.com

...

</VirtualHost>

nginx example which can be in the server block for port 80. This will redirect to your HTTPS port 443 server block.


server {                                                                                                                                                            
        listen 80;                                                                                                                                                  
        listen [::]:80;                                                                                                                                             

...
                                                                                                                                                                                                                                     
        location / {                                                                                                                      
        return 301 https://mydomain.com$request_uri?;                                                                                   
      }                                                                                                                          

...
                                                                                                                                 
} 

Hack on,

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.