301 two methods of redirection

Source: Internet
Author: User

From the perspective of search engine optimization, 301 redirection is the most feasible method for URL redirection. When the domain name of a website changes, the search engine only indexes the new website and transfers the original external links under the old address to the new one, in this way, the ranking of websites will not be affected by changes in the website. Similarly, when you use the 301 permanent redirection command to direct multiple domain names to the primary domain of the website, the ranking of the website will not have any negative impact.

In general, there are two ways to achieve 301 redirection.

Modify the. htaccess File

The Code is as follows:

 
  RewriteEngine OnRewriteCond %{HTTP_HOST} bkjia.com$ [NC]RewriteRule ^(.*)$ http://bkjia.com/$1 [R=301,L]RewriteCond %{HTTP_HOST} bkjia.info$ [NC]RewriteRule ^(.*)$ http://bkjia.com/$1 [R=301,L]
 

The key code is two sentences:

RewriteCond %{HTTP_HOST} bkjia.com$ [NC]RewriteRule ^(.*)$ http://bkjia.com/$1 [R=301,L]

The above domain name is the old domain name to be redirected, and the following is the domain name of the current website.

PHP redirection code

Create an index. php file, and then follow the code below to make simple modifications according to your own redirection requirements:

<?php$the_host = $_SERVER['HTTP_HOST'];$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';switch ($the_host){    case "www.bkjia.tk":    case "bkjia.tk":        $location = "Location: http://bkjia.com" . $request_uri;        break;    case "blog.bkjia.tk":        $location = "Location: http://blog.bkjia.com" . $request_uri;        break;    case "www.moiya.tk":    case "moiya.tk":        $location = "Location: http://bkjia.com";        break;    default:        $location = "Location: http://bkjia.com";        break;}header('HTTP/1.1 301 Moved Permanently');header($location);exit();?>

If you only need to redirect a domain name, you can simplify the code into the following form:

<? Php $ the_host = $ _ SERVER ['HTTP _ host']; // get the entered domain name $ request_uri = isset ($ _ SERVER ['request _ URI '])? $ _ SERVER ['request _ URI ']: ''; // you can specify if ($ the_host! = 'Bkjia. com ') // bkjia.com is my current domain name {header ('HTTP/1.1 301 Moved Permanently'); // issue the 301 header ('location: http://bkjia.com '. $ request_uri); // jump to my new domain name address exit () ;}?>

Note that the last exit () function must be written. I didn't write it at first, and the result can only be redirected to the homepage, such as http://www.bkjia.com/guestbook.

Finally, some details about redirection: If you want to redirect three Domain names, bind these three Domain names to my server as Addon Domain before redirection, and point these three domain names to the same folder. In this way, you only need to modify. htaccess file or index. PHP file. If there is no. htaccess file or index. php file, create a new one.

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.