Для чего это нужно?
В первую очередь, при изменении доменного имени сайта, необходимо оповестить поисковые системы о смене адреса сайта. Во-вторых, для склейки имени сайта с www и без него. В-третьих для быстрой передачи Page Rank на новый сайт.
PHP
Способ первый
1
2
3
4
5
|
<?php header( "HTTP/1.1 301 Moved Permanently" ); exit (); ?> |
Способ второй
1
2
3
4
|
Perl
Способ первый
1
2
|
$cgi = new CGI; |
Способ второй
1
2
3
4
5
|
#!/usr/bin/perl -w use strict; print "Status: 301 Moved Permanently\n" ; exit ; |
ASP.NET
Способ первый
1
2
3
4
5
6
|
<script runat= "server" > private void Page_Load(object sender, System.EventArgs e) { Response.Status = "301 Moved Permanently" ; } </script> |
Способ второй (с версии 4.0)
1
|
RedirectPermanent("http://www.example.com"); |
ASP
1
2
3
4
5
6
|
<%@ Language=VBScript %> <% Response.Status="301 Moved Permanently" Response.AddHeader "Location", "http://www.example.com/" response.end %> |
Ruby on Rails
1
2
3
4
|
def do_something headers["Status"] = "301 Moved Permanently" redirect_to "http://www.example.com/" end |
ColdFusion
1
2
|
<.cfheader statuscode="301" statustext="Moved Permanently"> <.cfheader name="Location" value="http://www.example.com"> |
Java (JSP)
1
2
3
4
5
|
<% response.setStatus(301); response.setHeader( "Connection" , "close" ); %> |
Веб-сервер Apache (.htaccess)
Способ первый (mod_alias, Redirect)
1
|
Redirect 301 / http: //www .example.com |
Способ второй (mod_alias, RedirectPermanent)
1
|
RedirectPermanent / http: //www .example.com |
Способ третий (mod_alias, Redirect permanent)
1
|
Redirect permanent / http: //www .example.com |
Способ четвертый (mod_alias, RedirectMatch)
1
|
RedirectMatch 301 ^(.*)$ http: //www .example.com/ |
Способ пятый (mod_rewrite)
1
2
3
4
|
Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteRule ^(.*)$ http: //www .example.com/$1 [R=301,L] |
Веб-сервер ngix
1
|
rewrite ^(.*)$ http://www.example.com$1 permanent; |