IIS and APACHE redirects HTTP to HTTPS

  • 2021-01-03 21:10:46
  • OfStack

IIS7

From Microsoft's official website to download HTTP rewrite module, installed after the restart IIS service, after open the IIS console, found that more than one component, the double click on the "URL rewrite", select "add rule" in the right form, and add a space rules, gave a name to the custom rules 1 feel (name), such as I call it "redirect to HTTPS", model as follows: (.*), add 1 condition, the condition input is {HTTPS}, matches the pattern, ^OFF$, and then configure the operation, the operation type is: redirection, the redirection to URL is: https://{HTTP_HOST}/{R:1}, the redirection type is: permanent 301.

After setting up, click "Apply" on the right and the URL rewrite will be configured.

After configuration, the contents of the web. config file in the root directory are as follows:


<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="redirect to HTTPS" enabled="true" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Apache http jump https configuration

Modify the.htaccess file to add the following lines to the file:


RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Another way of writing it is:


RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]

nginx configuration

rewrite method of nginx

Train of thought

This should be the easiest way to think about it, rewriting all http requests to https via rewrite

configuration


server { 
  listen 192.168.1.111:80; 
  server_name test.com; 
   
  rewrite ^(.*)$ https://$host$1 permanent; 
} 

After setting up the virtual host, you can rewrite all the requests of http:// test.com to https:// test.com

nginx 497 status code

error code 497

497 - normal request was sent to HTTPS

Explanation: When this virtual site only allows https access, nginx will report an error code of 497 when accessed with http

Train of thought

Use the error_page command to redirect the 497 status code link to the domain https:// test.com

configuration

[

server {
listen 192.168.1.11:443; # ssl port
listen 192.168.1.11:80; # User is used to http access, plus 80, followed by 497 status code to make it jump to port 443 automatically
server_name test.com;
# is 1 server{... } Enable ssl support
ssl on;
# Specify the certificate file in PEM format
ssl_certificate /etc/nginx/test.pem;
# Specify the private key file in PEM format
ssl_certificate_key /etc/nginx/test.key;

# Redirect http requests to https requests
error_page 497 https://$host$uri?$args;
}

]

index. html refresh the web page

Train of thought
The above two methods will consume the server resources, we use curl to visit baidu.com try 1, see how Baidu's company to achieve baidu.com to www.baidu.com jump

It can be seen that Baidu cleverly uses the refresh effect of meta to jump from ES141en. com to ES143en. baidu. com. So we can based on http: / / test com virtual host path also write 1 index. html, content is http https to jump

index.html

[

< html >
< meta http-equiv="refresh" content="0;url=https://test.com/" >
< /html >

]

nginx virtual host configuration

[

server {
listen 192.168.1.11:80;
server_name test.com;

location / {
# index. html is placed in the root directory of the virtual host listener
root /srv/www/http.test.com/;
}
# Redirect the 404 page to the home page of https
error_page 404 https://test.com/;
}

]

Afterword.
All the above three methods can be used to forcibly switch the http request to https request based on nginx, and you can evaluate the merits of 1 or choose according to the actual requirements.


Related articles: