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

How to realize automatic Jump from http to https in Apache

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

Share

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

Editor to share with you how Apache automatically jumps from http to https, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

There are several ways for Apache http to automatically jump to https. When your site uses HTTPS, you may want to redirect all HTTP requests (that is, requests on port 80) to HTTPS. At this point, you can do this in the following ways:

After enabling https, make sure that the previous http port can be opened. Port 80 of http has two URLs, so it is necessary to specify a https URL for both the original domain name with wwww and without www, and you need to redirect two 301s of Apache. This is actually very simple. The summer blog can add two 301s directly to the .htaccess file, as shown below:

Rewritecond% {http_host} ^ www.php.cn [nc] RewriteRule ^ (. *)? $https://www.php.cn/$1 [Rend301MagneL] RewriteCond% {SERVER_PORT}! ^ 443$ RewriteRule ^ (.*)? $https://www.php.cn/$1 [Red301MagneL

The first 301 is naturally redirected to the new https with www, while the redirect below 301 determines that if the port is not 80, the redirect will be carried out. In this case, the domain name with and without www will jump to a https URL together. Of course, this kind of site-wide method of doing 301 is more violent, usually we just need to make the main domain name 301. I am here because I enabled the original two domain names.

PHP Chinese website also provides some other ways for Apache http to jump to https for reference only:

Method 1

RewriteEngine On RewriteBase / RewriteCond% {SERVER_PORT} 80 RewriteRule ^ (. *) $https://www.php.cn/$1

Method two

RewriteEngine on RewriteCond% {SERVER_PORT}! ^ 443$ RewriteRule ^ (. *)? $https://%{SERVER_NAME}/$1 [Renew301 Magi L] # whole station jump

Method three

RewriteEngine on RewriteBase / yourfolder RewriteCond% {SERVER_PORT}! ^ 443 $# RewriteRule ^ (. *)? $https://%{SERVER_NAME}/$1 [Rene301Magic L] RewriteRule ^. * $https://%{SERVER_NAME}%{REQUEST_URI} [Run301Magi L] # above to jump for a directory, yourfolder is the directory name

Method 4

_ redirect 301 / your webpage https:// your host + webpage # to jump for a web page

Method 5

RewriteEngine on RewriteCond% {SERVER_PORT}! ^ 443$ RewriteCond% {REQUEST_URI}! ^ / tz.php RewriteRule (. *) https://%{SERVER_NAME}/$1 [R]

Explanation:

% {SERVER_PORT}-access port% {REQUEST_URI}-for example, if url is http: / / localhost/tz.php, it means / tz.php% {SERVER_NAME}-for example, if url is http: / / localhost/tz.php, it means localhost

The above rule means that if the port of the accessed url is not 443 and the access page is not tz.php, the RewriteRule rule is applied.

This makes it possible:

When you visit a page such as http: / / localhost/index.php or http: / / localhost/admin/index.php, you will automatically jump to https: / / localhost/index.php or https: / / localhost/admin/index.php, but you will not make any jump when you visit http: / / localhost/tz.php, that is, you can access both http: / / localhost/tz.php and https: / / localhost/tz.php.

PS: let's take a look at the method of automatically redirecting Apache from http to https. The details are as follows:

Modify the root directory .htaccess file

Options + FollowSymlinks RewriteEngine On#thinkphp remove index.php RewriteCond% {REQUEST_FILENAME}!-d RewriteCond% {REQUEST_FILENAME}!-f RewriteRule ^ (. *) $index.php/$1 [QSA,PT L] # http automatically jumps to https RewriteCond% {SERVER_PORT}! ^ 443 $# only matching the corresponding domain name will jump to RewriteCond% {SERVER_NAME} ^ hrsc.cc | www.hrsc.cc$ RewriteRule (. *) https://%{SERVER_NAME}/$1 [R] above is all the content of the article "how to automatically jump from http to https" in Apache Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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