How to set http to automatically jump to https in apache

https://www.cnblogs.com/niejunlei/p/5279677.html

How to set http to automatically jump to https?

In the apache environment, after configuring https, you need to set url redirection rules, so that http access to the website page is automatically transferred to https access.

1. First open url redirection support
 
  1) Open Apache/conf/httpd.conf, find #LoadModule rewrite_module modules/mod_rewrite.so and remove the # sign.   
 
  2) Find the <Directory> segment of your website directory, for example, my website directory is c:/www, find
    <Directory “C:/www”>
    …
    </Directory>
    Modify AllowOverride None to AllowOverride All
 
  3) Restart the apache service
 
2. Set redirection rules
 
  1) Put a .htaccess file in your website directory. In the windows environment, you cannot directly rename the file to .htaccess, you will be prompted to enter the file name. So we first create a new "New Text Document.txt" document, open Notepad, select Save As, select "All Files (*.*)" as the save type, enter ".htaccess" for the file name, and save. This generates a .htaccess file.

  2) The editor opens the .htaccess file and writes the following rules:
    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 the url is http://localhost/tz.php, it means /tz.php
    %{SERVER_NAME}——For example, if the url is http:/ /localhost/tz.php, refers to localhost

    The above rule means that if the port of the accessed url is not 443 and the accessed page is not tz.php, the RewriteRule rule is applied. This is achieved: when visiting pages such as http://localhost/index.php or http://localhost/admin/index.php, it will automatically jump to https://localhost/index.php or https:/ /localhost/admin/index.php, but will not make any jumps when visiting http://localhost/tz.php, that is to say http://localhost/tz.php and https://localhost/tz .php is accessible from both addresses.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325110494&siteId=291194637