This article introduces how PHP implements 301 permanent redirection to direct a domain name without www to a domain name with www.
When your site domain name changes or the webpage address changes (the webpage storage directory or the webpage file name changes), how can you ensure that the site's traffic from search engines will not be lost during the transition period, can I update the webpage address in the search engine results? 301 redirection is an ideal solution to this problem. It can direct users and search engines to the correct webpage.
The Code is as follows: |
Copy code |
<? Php $ The_host = $ _ SERVER ['HTTP _ host']; // obtain the entered Domain Name $ Request_url = isset ($ _ SERVER ['request _ URI '])? $ _ SERVER ['request _ URI ']: ''; // judge the part after the address If ($ the_host! = 'Www. bKjia. c0m') // This is the address of the previous domain name. {Header ('HTTP/1.1 301 Moved Permanently '); // issue the 301 header Header ('location: http://www.bKjia. c0m'. $ request_url); // jump to my new domain address } ?> |
1. Add the 301 redirection command to the. htaccess file.
The "mod_rewrite" technology is used, for example:
The Code is as follows: |
Copy code |
RewriteEngine on RewriteRule ^ (. *) $ http://www.bKjia. c0m/$1 [R = 301, L] |