301 Redirect – How to create Redirects in htaccess

Posted on Posted in 301 redirects

301 Redirect Codes:

#######################
The old .asp dynamic pages redirect to .php new pages (New site hosted in Linux server)
#########################

RewriteEngine On
Options +FollowSymLinks

DirectoryIndex home.php index.php index.html index.htm
RewriteCond %{QUERY_STRING} Page=CAT&CatID=0&SelID=5239
RewriteRule Default.asp http://www.showmeagain.com/search_products.php?mode=search&add_date=1 [R=301,L]

RewriteCond %{QUERY_STRING} Page=CAT&CatID=0&SelID=5750
RewriteRule Default.asp http://www.showmeagain.com/search_products.php?mode=search&on_sale=1 [R=301,L]

RewriteCond %{QUERY_STRING} Page=CAT&CatID=0&SelID=5886
RewriteRule Default.asp http://www.showmeagain.com/search_products.php?mode=search&last_chance_items=1 [R=301,L]

#############################

Redirect Old domain to new domain
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

########################

For domain.com to www.domain.com

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^peo-services.com [nc]
rewriterule ^(.*)$ http://www.peo-services.com/$1 [r=301,nc]
#########################

For index.htm to www.domain.com

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.htm
RewriteRule ^index.htm$ http://www.peo-services.com/ [R=301,L]

Redirecting index.php to Root using htaccess
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.domain.com/$1 [R=301,L]

#####################
For Old page to new page:

redirect 301 /contact.htm http://www.domain.com/contact-us.htm
#####################

Below are a Couple of methods to implement URL Redirection:

ColdFusion Redirect
<.cfheader statuscode=”301″ statustext=”Moved permanently”>
<.cfheader name=”Location” value=”http://www.new-url.com“>

HTTP 301 Redirect in ColdFusion
<CFHEADER statuscode=”301″ statustext=”Moved Permanently”>
<CFHEADER name=”Location” value=” http://www.new-url.com/“>
HTTP 301 Redirect in ASP-VBScript
<%@ Language=VBScript %>
<%
‘ Permanent redirection
Response.Status = “301 Moved Permanently”
Response.AddHeader “Location”, ” http://www.new-url.com/
Response.End
%>
########################

PHP Redirect
<?
Header( “HTTP/1.1 301 Moved Permanently” );
Header( “Location: http://www.new-url.com” );
?>

######################

HTTP 301 Redirect in PHP
<?php
// Permanent redirection
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://www.somacon.com/“);
exit();
?>

#######################

Redirection with Javascript
<html>
<head>
<script type=”text/javascript”>
window.location.href=’http://www.newdomain.com/’;
</script>
</head>
<body>
This page has moved to <a href=”http://newdomain.com/”>http://newdomain.com/</a>

</body>
</html>
Note that they syntax document.location=”; has been deprecated. Use the above code, or alternately, document.URL=”;.
####################

Redirection with META Refresh
<html>
<head>
<meta http-equiv=”refresh” content=”2;url=http://www.newdomain.com/”>
</head>
<body>
This page has moved to <a href=”http://newdomain.com/”>http://newdomain.com/</a>
</body>
</html>
####################

IIS Redirect (Windows Server)
• In internet services manager, right click on the file or folder you wish to redirect
• Select the radio titled “a redirection to a URL”.
• Enter the redirection page
• Check “The exact url entered above” and the “A permanent redirection for this resource”
• Click on ‘Apply’
######################

ASP Redirect
<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”;
Response.AddHeader(“Location”,”http://www.new-url.com/“);
%>

######################

ASP .NET Redirect
<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(“Location”,”http://www.new-url.com“);
}
</script>
##########################

JSP (Java) Redirect
<%
response.setStatus(301);
response.setHeader( “Location”, “http://www.new-url.com/” );
response.setHeader( “Connection”, “close” );
%>

######################

CGI PERL Redirect
$q = new CGI;
print $q->redirect(“http://www.new-url.com/“);
CGI PERL Redirect
$q = new CGI;
print $q->redirect(“http://www.new-url.com/“);
HTTP 301 Redirect in Perl
#!/usr/bin/perl      -w
use strict;
print “Status: 301 Moved Permanantlyn”;
print “Location: http://somewhere/page.htmnn“;
exit;

######################

Ruby on Rails Redirect
def old_action
headers[“Status”] = “301 Moved Permanently”
redirect_to “http://www.new-url.com/
#######################

302 Redirect Implemented in .htaccess File
 Assuming the web server allows it, it is easy to implement a 302 redirect in .htaccess.
######################

Redirecting a page
Redirect /file-name.html http://www.domain.com/temporary-directory/temporary-file-name.html
######################

Redirecting a directory
Redirect /directory http://www.domain.com/temporary-directory
####################

Redirecting an entire site
Redirect / http://www.temporary-domain.com/

######################

302 Redirect Implemented in a Server Script
 The script (typically a PHP, Perl or ASP program) will have to generate a 302 header.
Redirecting a page in PHP
header(“Location: http://www.domain.com/temporary-address/temporary-file-name.html“);
exit();
?>

Srinivas Katam

Do you need help growing your business? Srinivas Katam is a Digital Marketing Strategist and Consultant with over 200 successful SEO and Social Media client engagements completed. I’ve worked with brands across the world as a digital marketing consultant to deliver revenue growth from content-focused inbound marketing campaigns. He has been partnering with companies like yours for more than 13+ years to provide more traffic with better results. He is an Innovative Marketer who specializes in digital marketing, specifically in social media, SEO and online strategy.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInPinterestGoogle PlusDiggStumbleUponYouTubeRedditDelicious

Leave a Reply

Your email address will not be published. Required fields are marked *