Php 301 redirection implementation methods

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:

<ifmodule mod_rewrite.c="">RewriteEngine OnRewriteCond %{HTTP_HOST} nowamagic.com$ [NC]RewriteRule ^(.*)$ http://nowamagic.net/$1 [R=301,L]RewriteCond %{HTTP_HOST} nowamagic.info$ [NC]RewriteRule ^(.*)$ http://nowamagic.net/$1 [R=301,L]</ifmodule>

The key code is two sentences:

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

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

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.nowamagic.tk":    case "nowamagic.tk":        $location = "Location: http://nowamagic.net" . $request_uri;        break;    case "blog.nowamagic.tk":        $location = "Location: http://blog.nowamagic.net" . $request_uri;        break;    case "www.moiya.tk":    case "moiya.tk":        $location = "Location: http://nowamagic.net";        break;    default:        $location = "Location: http://nowamagic.net";        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! = 'Nowamagic. net ') // nowamagic.net is my current domain {header ('HTTP/1.1 301 Moved Permanently'); // issue the 301 header ('location: http://nowamagic.net '. $ request_uri); // jump to my new domain name address exit () ;}?>

Note that the final 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.nowamagic.net/guestbook.

Finally, some details about redirection: If you want to redirect three domain names, you must use these three domain names as Addon before redirecting.
Domain is bound to my server and directed to the same folder. In this way, you only need to modify the. htaccess file or the index. php file in this folder. If there is no. htaccess file or index. php file, create a new one. Note that the final 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.nowamagic.net/guestbook.
Address: http://www.nowamagic.net/librarys/veda/detail/1538

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.