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

Mysql Chinese sorting rules description

When using MySQL, we often sort and query a field...

Detailed explanation of the execution principle of MySQL kill command

Table of contents Kill instruction execution prin...

Vue mobile terminal determines the direction of finger sliding on the screen

The vue mobile terminal determines the direction ...

vue.js downloads pictures according to picture url

Recently, when I was working on a front-end vue.j...

A brief discussion on using Vue to complete the mobile apk project

Table of contents Basic Configuration Entry file ...

HTML sample code for implementing tab switching

Tab switching is also a common technology in proj...

Manually implement the two-way data binding principle of Vue2.0

In one sentence: Data hijacking (Object.definePro...

About browser compatibility issues encountered and solutions (recommended)

Preface: Last Sunday, a senior asked me to help m...

Perfect solution to the problem of webpack packaging css background image path

Inside the style tag of the vue component, there ...

Vue uses the video tag to implement video playback

This article shares the specific code of Vue usin...

How to create your own image using Dockerfile

1. Create an empty directory $ cd /home/xm6f/dev ...

MySQL Series 3 Basics

Table of contents Tutorial Series 1. Introduction...

CentOS 6.5 configuration ssh key-free login to execute pssh command explanation

1. Check and install pssh, yum list pssh 2. Becau...