A 301 redirect is the search friendly method of advising search engines where they can discover the new location for the current URL. When moving/removing content with backlinks a 301 is strongly recommended to maintain backlink equity, this will ensure any search rankings and backlinks a webpage previously gained are retained on the new webpage.
The code examples below show how to perform this 301 redirect from one URL to another across various common web platforms/languages.
Apache 301 Redirect
With Mod_Rewrite:RewriteRule ^page.html$ http://newsite.com/page.html [R=301,L]
With Mod_Alias:Redirect 301 /page.html http://newsite.com/page.html
JSP/Java 301 Redirectresponse.setStatus(301);
response.setHeader( "Location", "http://newsite.com/page.html" );
response.setHeader( "Connection", "close" );
PHP 301 Redirectheader("HTTP/1.1 301 Moved Permanently");
header("Location: http://newsite.com/page.html");
exit();
Node.JS 301 Redirectvar http = require("http");
http.createServer(function (req, res) {
res.writeHead(301, {"Location": "http://newsite.com/page.html"});
res.end();
}).listen(8080);
ASP 301 Redirect<%
Response.Status="301 Moved Permanently"
Response.AddHeader='Location','http://newsite.com/page.html'
%>
CGI PERL 301 Redirectuse CGI;
my $q = CGI->new();
print $q->redirect(
-location => 'http://newsite.com/page.html',
-status => 301,
);
Coldfusion 301 Redirect<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://newsite.com/page.html">
Python (with Django) 301 Redirectfrom django import http
def view(request):
return http.HttpResponsePermanentRedirect ('http://newsite.com/page.html')
ROR (Ruby on Rails) 301 Redirectdef old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.domain.com/"
end
Stay up to date
Receive all the latest news, events, and insights on B2B tech, marketing, and communications with Clarity’s free monthly newsletter.
Share this
MORE
INSIGHT
Fearless tactics to achieve your strategic success
As a consultancy, our full-funnel marketing and communications solutions are designed to fearlessly deliver business results across multiple industries and service areas.