Php domain name 301 redirection program code

Source: Internet
Author: User
Tags website domain names

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 indicates temporary transfer (Temporarily Moved ),

Example

In php, normal temporary redirection is usually used:
 

The code is as follows: Copy code
<? Php
Header ("Location: your_dest_url ");
?>

Simplest practice

The code is as follows: Copy code

$ The_host = $ _ SERVER ['http _ host']; // Obtain the current domain name
If ($ the_host! = 'Noniu. Com') // determines if 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
Header ("Location: 111cn.net) // jump to the expected domain name
Exit ();
}

This can also be implemented, for example, to jump to www.111cn.net on 111cn.net, that is, to make all the pages use a URL with www

The code is as follows: Copy code

<? Php
$ The_host = $ _ SERVER ['http _ host']; // Obtain the current domain name
$ The_url = isset ($ _ SERVER ['request _ URI '])? $ _ SERVER ['request _ URI ']: ''; // judge the part after the address
$ The_url = strtolower ($ the_url); // convert the English letters to lowercase letters.
If ($ the_url = "/index. php") // You can check whether the homepage is correct.
{
$ The_url = ""; // if the homepage is displayed, the value is null.
}
If ($ the_host! = 'Www .111cn.net ') // if the domain name is not a url with www, perform the following 301 redirect.
{
Header ('http/1.1 301 Moved Permanently '); // issue the 301 header
Header ('Location: http://www.111cn.net '. $ the_url); // jump to the URL with www
}
?>

7. Switch to code 301 in Apache

Create a new. htaccess file and enter the following content (mod_rewrite must be enabled ):

1) switch the domain name without WWW to the domain name with WWW

The code is as follows: Copy code

Options + FollowSymLinks
RewriteEngine on
RewriteCond % {HTTP_HOST} ^ lesishu.cn [NC]
RewriteRule ^ (. *) $ http://www.111cn.net/$1 [L, R = 301]

2) redirect to a new domain name

The code is as follows: Copy code

Options + FollowSymLinks
RewriteEngine on
RewriteRule ^ (. *) $ http://www.111cn.net/$1 [L, R = 301]

Wordpres root directory 301 jump

The code is as follows: Copy code

# BEGIN WordPress
Options + FollowSymlinks
RewriteEngine on
RewriteCond % {http_host} ^ 111cn.net [NC]
RewriteRule ^ (. *) $ http://www.111cn.net/$1 [L, R = 301]
RewriteCond % {THE_REQUEST} ^ [A-Z] {3, 9}/index. php HTTP/
RewriteRule ^ index. php $ http://www.111cn.net/[R = 301, L]
RewriteRule ^ index. php $-[L]
RewriteCond % {REQUEST_FILENAME }! -F
RewriteCond % {REQUEST_FILENAME }! -D
RewriteRule./index. php [L]
# END WordPress

Test

 

Analysis:

Php 301 jump code is only applicable to full php page or single page jump, apache/iis 301 jump applies to a large number of website domain names 301 jump, both of them share 301 in common, but each of them has advantages. You can choose one based on your own situation.

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.