Single page 301 redirects on different platforms

William Julian-Vicary

Strategy & Innovation Director

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 Redirect
response.setStatus(301);
response.setHeader( "Location", "http://newsite.com/page.html" );
response.setHeader( "Connection", "close" );

PHP 301 Redirect
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://newsite.com/page.html");
exit();

Node.JS 301 Redirect
var 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 Redirect
use 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 Redirect
from django import http
def view(request):
return http.HttpResponsePermanentRedirect ('http://newsite.com/page.html')

ROR (Ruby on Rails) 301 Redirect
def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.domain.com/"
end

Related Posts

A marketer’s guide to mobile-first indexing

In Google’s ever-continuing efforts to follow user trends and make the web more mobile friendly, mobile-first indexing was rolled out. Read this guide to find out what it is and how you can use it to it’s best ability for marketing and website success.


2 months ago

The step-by-step guide on how to prepare for Google's Page Experience update

Having good user experience (UX) is something every webmaster should strive for. With Google's Page Experience update rolling out in May 2021, we’ve outlined what this is and how you can prepare for it.


2 months ago

Fine-tune your content in 2021 with these SEO tips and trends

As we take our first steps into 2021, now’s the time to identify the most effective ways to use content to connect your business with its audiences. Not only will this help you get ahead of the competition, but after a curveball year in 2020, it’ll ensure you’re on the right track if your audiences’ behaviours online have changed at all.


4 months ago

Inside Screaming Frog: Top Takeaways From The Workshop

Ensuring that your URLs can be crawled and indexed is a cornerstone of SEO. If Google can’t find that lovely content you want to get in front of your customers, what’s the point in writing it, right?


2 years ago

Our top takeaways from BrightonSEO

Learn how to implement a 301 redirect from one page to another for different platforms, such as PHP, Java, Apache, ASP, CGI, Python, ColdFusion & Ruby.


2 years ago

Google changes affecting local queries

As Google continuously updates in order to keep up with our ever-changing world of tech, we ask, what are the recent changes affecting local queries?


3 years ago

Drag