Multiple ways for Apache to automatically jump from http to https
- 2020-06-23 02:40:08
- OfStack
This article will share several ways in which Apache http automatically jumps to https. Once your site is using HTTPS, you may want to redirect all HTTP requests (that is, port 80 requests) to HTTPS. You can do this in the following ways:
After you enable https, and ensure that before http port can open, http port 80 is two sites, so this leads to need to bring the original wwww and without www domain name specifies a https url above at the same time, you need to do two Apache 301 redirects, and this is actually very simple, blog is directly in the summer htaccess file to add two 301 can, as shown below:
rewritecond %{http_host} ^www.php.cn [nc]
RewriteRule ^(.*)?$ <a href="https://www.php.cn/" target="_blank">https://www.php.cn/</a>$1 [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ <a href="https://www.php.cn/" target="_blank">https://www.php.cn/</a>$1 [R=301,L
First 301 naturally is to bring www jump to new https, above and below the 301 redirect is to judge if the port is not 80 words, then redirect, so, with www and without www domain was 1 to jump to https 1 url above, of course, this is a method of total station for 301 is more violent, normally we just do the main domain name 301, I came here because you enable two domain names.
PHP Chinese also mobile 1 other Apache http jump to https method, for reference only:
Method 1
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ <a href="https://www.php.cn/" target="_blank">https://www.php.cn/</a>$1 [R=301,L]
Method 2
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L]# Site jump
Methods 3
RewriteEngine on
RewriteBase /yourfolder
RewriteCond %{SERVER_PORT} !^443$
#RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L]
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
# Above to a directory jump, yourfolder That's the directory name
Methods 4
redirect 301 / Your web page https:// Your host + Web page
# To redirect to a web page
Methods 5
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/tz.php
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]
Explanation:
%{SERVER_PORT} - Access to the port
%{REQUEST_URI} - For example if url is http: //localhost/tz.php , is /tz.php
%{SERVER_NAME} - For example if url is http: //localhost/tz.php , is localhost
The above rule means that if the url accessed is not port 443 and the page visited is not ES52en.php, then the rule RewriteRule is applied.
This is how it works:
Visited http: / / localhost index php or http: / / localhost admin/index php while waiting for the page will automatically jump to https: / / localhost index php or https: / / localhost admin/index php, but access http: http: //localhost/ tz.php and https: //localhost/ tz.php both can be accessed.
PS: Next, take a look at the method of Apache automatically jumping from http to https. The details are as follows:
Modify the root.htaccess file
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
#thinkphp To get rid of index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
#http Automatic jump to https
RewriteCond %{SERVER_PORT} !^443$
# Only if the corresponding domain name is matched will it jump
RewriteCond %{SERVER_NAME} ^hrsc.cc|www.hrsc.cc$
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R]
</IfModule>
conclusion