Php domain name 301 redirection program code

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.