Apachehttp automatically redirects to https. when your site uses HTTPS, you may want to redirect all HTTP requests (that is, requests on port 80) to HTTPS. In this case, you can use the following methods:
After https is enabled, make sure that the previous http port can be opened. The http port 80 has two URLs, therefore, you need to specify an https URL for both the original domain name with and without www.
To achieve the 301 redirection of two Apache servers, this is actually very simple. the practice of a summer blog is to add two 301 servers directly to the. htaccess file, as shown below:
rewritecond %{http_host} ^www.php1.cn [nc] RewriteRule ^(.*)?$ https://www.php1.cn/$1 [R=301,L] RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^(.*)?$ https://www.php1.cn/$1 [R=301,L]
The first 301 is naturally redirected to the new https with www, and the following 301 redirection is used to determine that if the port is not 80, the redirection will be performed. in this case, A domain name with or without www jumps to an https website together. of course, this method of 301 is violent, generally, we only need to set the primary domain name to 301. I am using the original two domain names.
PHP 文 also provides some other methods for redirecting from Apache http to https. they are for reference only:
Method 1
RewriteEngine On RewriteBase / RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.php1.cn/$1 [R=301,L]
Method 2
RewriteEngine on RewriteCond %{SERVER_PORT} !^443$ RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [R=301,L]
# Whole site jump
Method 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]
# Yourfolder is the directory name for a specific directory.
Method 4
Redirect 301/your webpage https: // Your Host + webpage
# Jump to a webpage
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 the url is http: // localhost/tz. php, it indicates/tz. php.
% {SERVER_NAME} -- for example, if the url is http: // localhost/tz. php
The preceding rule indicates that if the port of the Accessed url is not 443 and the access page is not tz. php, the RewriteRule rule is applied.
In this way, http: // localhost/index is accessed. php or http: // localhost/admin/index. php and other pages will automatically jump to https: // localhost/index. php or https: // localhost/admin/index. php, but access http: // localhost/tz. php does not perform any redirection, that is, http: // localhost/tz. php and https: // localhost/tz. both php addresses can be accessed.