Asp.net php asp jsp 301 redirection code

Source: Internet
Author: User
Tags subdomain name to domain
Technorati labels: Asp.net, PHP, ASP, JSP, 301 redirection

IntroductionProgramThe system implements 301 redirectionCode:
1. Linux host redirection
Godaddy's liunx host, Godaddy itself supports Apache, so you can directly create. the htaccess file is enough. Generally, it cannot be created locally. when using htaccess, you can create a TXT file and rename it to ". htaccess. 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.wecanwecan.com as an example.
1.1 Transfer without WWW domain name to www Domain Name
Rewriteengine on
Rewritecond % {http_host} ^ wecanwecan.com [Nc]
Rewriterule ^ (. *) $ http://www.wecanwecan.com/#1 [R = 301, NC]
1.2 whole site 301 redirection
Options + followsymlinks
Rewriteengine on
Rewritecond % {http_host} ^ wecanwecan.com [Nc]
Rewriterule ^ (. *) $ http://www.wecanwecan.com/#1 [L, r = 301]
Rewritecond % {http_host} ^ www.wecanwecan.com [Nc]
Rewriterule ^ (. *) $ http://wecancan.com/#1 [L, r = 301]

The other is in index. php under the root directory.

Header ("HTTP/1.1 301 moved permanently ");
Header ("Location: http://wecanwecan.com /");
Exit ();

2. asp host 301 redirection

Add the following lines to the top of index. asp or default. asp:
The Code is as follows:

<%
Response. Status = "301 moved permanently"
Response. addheader "location", "www.wecanwecan.com"
Response. End
%>

3. Asp.net host redirection 301

ASP. NET:

Response. Status = "301 moved permanently ";

Response. addheader ("location", "http://www.wecanwecan.com ");

}

I encapsulate it in a class:

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;

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

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.

<? 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:

<? PHP
//-----------------------------------
// Redirect 301
Include ('include/301. Inc. php ');

?>

5. 301 redirection of JSP

For example, the page article. jsp

[Code]

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %>
<%
Response. setstatus (httpservletresponse. SC _moved_permanently );
Response. setheader ("location", "/other. jsp ");

Return;
%>

[Code]

How can we achieve 301 redirection in Apache?

Edit the. htaccess method.
Note: Before setting 301 redirection, you must back up the. htaccess file in the corresponding directory.
1. Redirect domain.com to www.domain.com
This type of redirection aims to make the domain name unique and is required by the website Seo. The redirection from www.domain.com to domain.com is also for the same reason, but the form is different.
Open the. htaccess file and add the following rules. (The following rules apply to the primary domain name. The subdomain name must be modified)

Rewriteengine on
Rewritecond % {http_host }! ^ Www.domain.com $ [Nc]
Rewriterule ^ (. *) $ http://www.domain.com/#1 [L, r = 301]

2. Redirect www.domain.com to domain.com

Rewriteengine on
Rewritecond % {http_host }! ^ Domain.com $ [Nc]
Rewriterule ^ (. *) $ http://domain.com/#1 [L, r = 301]

3. Redirect olddomain.com to www.newdomain.com

Rewriteengine on
Rewritecond % {http_host }! Olddomain.com $ [Nc]
Rewriterule ^ (. *) $ http://www.newdomain.com/#1 [L, r = 301]

4. Redirect olddomain.com to newdomain.com

Rewriteengine on
Rewritebase/
Rewritecond % {http_host }! Olddomain.com $ [Nc]
Rewriterule ^ (. *) $ http://newdomain.com/#1 [L, r = 301]

5. Redirect domain.com/file/file.php to otherdomain.com/otherfile/other.php

Rewritecond % {http_host} ^ www.domain.com $
Rewriterule ^ file/file. php $ http://www.otherdomain.com/otherfile/other.php [R = 301, l]

Related Article

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.