Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

IIS and APACHE to redirect HTTP to HTTPS

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces IIS and APACHE to achieve HTTP redirection to HTTPS, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Apache http Jump https configuration

Modify the .htaccess file by adding the following lines to the file:

RewriteEngine OnRewriteBase / RewriteCond% {HTTPS}! = onRewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [LMagne Renew301]

Another way to write it is:

RewriteEngine onRewriteBase / RewriteCond% {SERVER_PORT}! ^ 443$ RewriteRule (. *) https://%{SERVER_NAME}/$1

Nginx configuration

Rewrite method of nginx

Train of thought

This should be the easiest way for everyone to rewrite all http requests to https via rewrite.

Configuration

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

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

497 status code of nginx

Error code 497

497-normal request was sent to HTTPS

Explanation: when only https is allowed to access this virtual site, nginx will report a 497 error code when accessing with http

Train of thought

Use the error_page command to redirect the link of 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 listen 80; # users are accustomed to access it with http, plus 80, followed by 497 status code to automatically jump to port 443

Server_name test.com

# enable ssl support for a server {.}

Ssl on

# specify a certificate file in PEM format

Ssl_certificate / etc/nginx/test.pem

# specify a 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 refreshes the web page

Train of thought

Both of the above methods will consume the resources of the server. Let's visit baidu.com with curl to see how Baidu's company can jump from baidu.com to www.baidu.com.

We can see that Baidu skillfully uses the refresh function of meta to jump baidu.com to www.baidu.com. Therefore, we can also write an index.html under the virtual host path based on http://test.com, which is the jump from http to https.

Index.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 where the virtual host listens

Root / srv/www/http.test.com/

}

# redirect the page of 404 to the home page of https

Error_page 404 https://test.com/;

}

About IIS and APACHE to achieve HTTP redirection to HTTPS to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report