IIS and APACHE implement HTTP redirection to HTTPS

IIS and APACHE implement HTTP redirection to HTTPS

IIS7

Download the HTTP Rewrite module from Microsoft's official website. After the installation is complete, restart the IIS service. Then open the IIS console and you will find an additional component. Double-click "URL Rewrite" and select "Add Rule" in the right window. Add a blank rule and give the rule a custom name (you can name it as you like). For example, I call it "redirect to HTTPS" and the pattern is (.*). Add a condition and enter {HTTPS} to match the pattern. The pattern is ^OFF$. Then configure the operation. The operation type is: redirect. The redirection URL is: https://{HTTP_HOST}/{R:1}. The redirection type is: permanent 301.

After completing the settings, click "Apply" on the right and the URL rewriting is configured.

After configuration, the content of the web.config file in the root directory is 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 to https configuration

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

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

Another way to write it is:

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

nginx configuration

nginx rewrite method

Ideas

This should be the easiest method for everyone to think of. All http requests can be rewritten to https through rewrite

Configuration

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

After setting up this virtual host, you can rewrite all requests from 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 a 497 error code when accessed with http

Ideas

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

Configuration

server {
listen 192.168.1.11:443; #ssl port
listen 192.168.1.11:80; #Users are used to access via http, add 80, and then use the 497 status code to automatically jump to port 443
server_name test.com;
# Enable SSL support for a server {......}
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

Idea Both of the above methods will consume server resources. Let's use curl to visit baidu.com and see how Baidu achieves the jump from baidu.com to www.baidu.com

It can be seen that Baidu cleverly uses the refresh function of meta to jump baidu.com to www.baidu.com. Therefore, we can also write an index.html based on the virtual host path of http://test.com, and the content is the jump from http to https

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 monitor
root /srv/www/http.test.com/;
}
#Redirect the 404 page to the https homepage
error_page 404 https://test.com/;
}

Postscript All three methods mentioned above can be used to force http requests to jump to https requests based on nginx. You can evaluate the pros and cons or choose according to actual needs.

You may also be interested in:
  • How to implement http redirection to https in IIS 7
  • IIS7/IIS7.5 URL Rewriting HTTP Redirection to HTTPS
  • Set HTTP access to redirect to HTTPS in IIS

<<:  Detailed explanation of using javascript to handle common events

>>:  MySQL common test points for the second-level computer exam 8 MySQL database design optimization methods

Recommend

Detailed installation tutorial for MySQL zip archive version (5.7.19)

1. Download the zip archive version from the offi...

Several ways to solve the 1px border problem on mobile devices (5 methods)

This article introduces 5 ways to solve the 1px b...

JavaScript Dom Object Operations

Table of contents 1. Core 1. Get the Dom node 2. ...

Solution to the problem that the mysql8.0.11 client cannot log in

This article shares with you the solution to the ...

How to configure CDN scheduling using Nginx_geo module

Introducing the Geo module of Nginx The geo direc...

Summary of four ways to loop through an array in JS

This article compares and summarizes four ways of...

JavaScript implementation of the Game of Life

Table of contents Concept Introduction Logical ru...

MySQL full-text search Chinese solution and example code

MySQL full text search Chinese solution Recently,...

Font Treasure House 50 exquisite free English font resources Part 1

Designers have their own font library, which allo...

Mysql dynamically updates the database script example explanation

The specific upgrade script is as follows: Dynami...

Analysis of MySQL joint index function and usage examples

This article uses examples to illustrate the func...

Pure CSS to modify the browser scrollbar style example

Use CSS to modify the browser scroll bar style ::...

Vue scaffolding learning project creation method

1. What is scaffolding? 1. Vue CLI Vue CLI is a c...

2 reasons why html-css tag style setting does not work

1 CSS style without semicolon ";" 2 Tags...