In php, page 301 is redirected. We use the header () function to send status code 301 and then jump to the specified page. the implementation method is very simple. note: 301,302 is an HTTP status code, indicating that a URL has been transferred. no... in php, page 301 is redirected. We use the header () function to send status code 301 and then jump to the specified page. the implementation method is very simple.
Note:
301,302 is an HTTP status code, indicating that a URL has been transferred. The difference is:
301 redirect: 301 represents permanent transfer (Permanently Moved), 302 redirect: 302 represents temporary transfer, Temporarily Moved.
For example, normal temporary redirects in php are usually used. the code is as follows:
The code is as follows:
$ The_host = $ _ SERVER ['http _ host']; // Obtain the current domain name if ($ the_host! = 'Noniu. com ') // Determine whether the obtained domain name is what you want (that is, the domain name after targeting) {header ("HTTP/1.1 301 Moved Permanently "); // issue the 301 header ("Location: phprm.com) // jump to the expected domain name exit ();}
This can also be implemented, such as phprm.com, to jump to www.phprm.com, that is, to make all the pages use the URL with www, the code is as follows:
Apache 301 redirection code
Create a new. htaccess file and enter the following content. you must enable mod_rewrite.
1) switch the domain name without WWW to the domain name with WWW. the code is as follows:
Options +FollowSymLinks RewriteEngine on RewriteCond %{HTTP_HOST} ^lesishu.cn [NC] RewriteRule ^(.*)$ http://www.phprm.com /$1 [L,R=301]
2) redirect to the new domain name. The code is as follows:
Options +FollowSymLinks RewriteEngine on RewriteRule ^(.*)$ http://www.phprm.com /$1 [L,R=301]
The code for redirecting from the wordpres root directory 301 is as follows:
# BEGIN WordPress Options +FollowSymlinks RewriteEngine on RewriteCond %{http_host} ^phprm.com [NC] RewriteRule ^(.*)$ http://www.phprm.com /$1 [L,R=301] rewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.php HTTP/ rewriteRule ^index.php$ http://www.phprm.com / [R=301,L] RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress
Analysis: php 301 redirection code is only applicable to full php pages or single pages. apache/iis 301 redirection is applicable to a large number of website domain 301 redirects, both of them have 301 in common, but each of them has advantages. you can choose one based on your own situation.
Article link:
Save this article for favorites!