Asp.net php jsp asp 301 redirection implementation code

Source: Internet
Author: User

There are many tutorials on how to perform 301 redirection on the internet, whether it is full-site redirection or single-page redirection. The following uses my www. bKjia. c0m is used as an example, because you often do not have an independent server or your apache does not support it. for haccess files and other reasons, you have to use the script language for instance 301 redirection.

There are many tutorials on how to perform 301 redirection on the internet, whether it is full-site redirection or single-page redirection. The following uses my www. bKjia. c0m is used as an example, because you often do not have an independent server or your apache does not support it. for haccess files and other reasons, you have to use the script language for instance 301 redirection.
1.1 Transfer without www domain name to www Domain Name
Copy the Code as follows:

Rewriteengine on
Rewritecond % {http_host} ^ bKjia. c0m [nc]
Rewriterule ^ (. *) $ http://www.bKjia. c0m/$1 [r = 301, nc]

1.2 whole site 301 redirection
Copy the Code as follows:

Options + followsymlinks
Rewriteengine on
Rewritecond % {http_host} ^ bKjia. c0m [nc]
Rewriterule ^ (. *) $ http://www.bKjia. c0m/$1 [l, r = 301]
Rewritecond % {http_host} ^ www. bKjia. c0m [nc]
Rewriterule ^ (. *) $ http: // bKjia. c0m/$1 [l, r = 301]

In the index. php tutorial under the root directory
Copy the Code as follows:

Header ("http/1.1 301 moved permanently ");
Header ("location: http: // bKjia. c0m /");
Exit ();

2. asp tutorial host 301 redirection
Add the following lines to the top of index. asp or default. asp:
The Code is as follows:
Copy the Code as follows:

<%
Response. status = "301 moved permanently"
Response. addheader "location", "www. bKjia. c0m"
Response. end
%>

3. asp.net tutorial host 301 redirection
Asp. net:

Response. status = "301 moved permanently ";
Response. addheader ("location", "http://www.bKjia. c0m ");
}


I encapsulate it in a class:
Copy the Code as follows:

Using system;
Using system. collections. generic;
Using system. text;
Using system. web. ui;
Using system.web.ui.html controls;
Namespace classlib
{
Public class urlclass
{
Private bool flag301 = false; // whether to start 301
Private bool isindex = false; // whether to return to the home page or retain it on the current page
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "fl"> enable 301 </param>
/// <Param name = "page"> page </param>
/// <Param name = "strurl"> Format: www.xxx.com </param>
Public urlclass (bool fl, page, string strurl)
{
Flag301 = fl;
Url301 (page, strurl );
}
/// <Summary>
/// Return to the home page
/// </Summary>
/// <Param name = "page"> </param>
/// <Param name = "strurl"> Format: www.xxx.com </param>
Public void url301 (page, string strurl)
{
// Redirect 301
If (page. request. url. dnssafehost! = Strurl & flag301 = true)
{
Page. response. clear ();
Page. response. statuscode = 301;
Page. response. status = "301 movedpermanently ";
Page. response. addheader ("location", "http: //" + strurl );
Page. response. end ();
}
}
}
}

4. php 301 redirection
Copy the Code as follows:

Header ('HTTP/1.1 301 moved permanently '); // issue the 301 header
Header ('location: http: // www. '. $ strdomain. $ request_uri); // jump to my new domain name address

I used the 301. inc. php file to write the 301 code, which can be referenced in other file headers.
Copy the Code as follows:

<? Php
//-----------------------------------
// Redirect 301
$ Strdomain = "chinawecan.com ";
$ The_host = $ _ server ['HTTP _ host']; // obtain the entered Domain Name
$ Request_uri = isset ($ _ server ['request _ uri '])? $ _ Server ['request _ uri ']: ''; // judge the part after the address
If ($ the_host! = 'Www. '. $ strdomain) // This is my previous Domain Name
{
/* "! = "Is not completely equal to the meaning, you can also use"! = "Not equal to, in this way, you can replace the previous domain name,
Including gcxirang.com, www.gcxirang.com, and all my gcidc.net in the new domain name are redirected to www.gcidc.net */
Header ('HTTP/1.1 301 moved permanently '); // issue the 301 header
Header ('location: http: // www. '. $ strdomain. $ request_uri); // jump to my new domain name address
}
//----------------------------------
?>

Reference:
Copy the Code as follows:

<? Php
//-----------------------------------
// Redirect 301
Include ('include/301. inc. php ');
?>

5. 301 redirection in jsp tutorial
For example, the page article. jsp
[Code]
<% @ Page language = "java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %>
<%
Response. setstatus (https tutorial ervletresponse. SC _moved_permanently );
Response. setheader ("location", "/other. jsp ");
Return;
%>

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.