301 redirection code collection (IIS, ASP, PHP, ASP. Net, APACHE)

Time:2019-10-20

1. 301 settings under IIS

Internet Information Service Manager – > virtual directory – > redirect to URL, enter the target URL to be redirected, and select “permanent redirection of resources”.

In IIS, you can also install the ISAPI rewrite component to realize the function of mod’u rewrite in Apache. For details, see ISAPI rewrite 3 download and common 301 rules.

2. 301 redirection code under ASP

<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “106/”
%>

3. 301 redirection code under ASP. Net

<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(”Location”,”  106/“);
}
</script> 

4. 301 redirection code in PHP

header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: 106/”);
exit(); 

5. 301 redirection code under CGI Perl

$q = new CGI;
print $q->redirect(””);

6. 301 redirection code under JSP

<%
response.setStatus(301);
response.setHeader( “Location”, “” );
response.setHeader( “Connection”, “close” );
%>

7. 301 redirection code under Apache

Create a new. Htaccess file and enter the following (MOD? Rewrite needs to be turned on):

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

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^embeta.com [NC]
RewriteRule ^(.*)$    $1 [L,R=301]

2) redirect to new domain name

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$  $1 [L,R=301]

3) use regular to turn 301 to realize pseudo-static

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news-(.+)\.html1
Turn the address of news. PHP? Id = 123 to news-123.html

8. Configure 301 redirection in vhosts.conf under Apache

In order to realize URL normalization, SEO usually shifts the domain name without WWW to the domain name with www. the configuration in vhosts.conf is as follows:

<VirtualHost *:80>
ServerName DocumentRoot /home/embeta
</VirtualHost>
<VirtualHost *:80>
ServerName embeta.com
RedirectMatch permanent ^/(.*)    $1
</VirtualHost>

In addition to the above two methods under Apache, there are other configuration methods and optional parameters. It is recommended to read the Apache documentation.