- 301 REDIRECT
301 redirect is the best method to preserve your current search engine rankings when redirecting web pages or a web site. The code "301" is interpreted as "moved permanently". After the code, the URL of the missing or renamed page is noted, followed by a space, and then followed by the new location or file name. You implement the 301 redirect by creating a .htaccess file.
- .htaccess file
When a visitor/spider requests a web page, your web server checks for a .htaccess file. The .htaccess file contains specific instructions for certain requests, including security, redirection issues and how to handle certain errors.
- How to implement the 301 Redirect
1. To create a .htaccess file, open notepad, name and save the file as .htaccess (there is no extension).
2. If you already have a .htaccess file on your server, download it to your desktop for editing.
2. If you already have a .htaccess file on your server, download it to your desktop for editing.
3. Place this code in your .htaccess file:
redirect 301 /old/old.htm http://www.you.com/new.htm
redirect 301 /old/old.htm http://www.you.com/new.htm
4. If the .htaccess file already has lines of code in it, skip a line, then add the above code.
5. Save the .htaccess file
6. Upload this file to the root folder of your server.
7. Test it by typing in the old address to the page you've changed. You should be immediately taken to the new
location.
5. Save the .htaccess file
6. Upload this file to the root folder of your server.
7. Test it by typing in the old address to the page you've changed. You should be immediately taken to the new
location.
Other ways to implement the 301 redirect:
1. To redirect ALL files on your domain use this in your
.htaccess file if you are on a unix web server:
redirectMatch 301 ^(.*)$ http://www.domain.com
redirectMatch permanent ^(.*)$ http://www.domain.com
You can also use one of these in your .htaccess file:
redirect 301 /index.html http://www.domain.com/index.html
redirect permanent /index.html http://www.domain.com/index.html
redirectpermanent /index.html http://www.domain.com/index.html
This will redirect "index.html" to another domain using a
301-Moved permanently redirect.
1. To redirect ALL files on your domain use this in your
.htaccess file if you are on a unix web server:
redirectMatch 301 ^(.*)$ http://www.domain.com
redirectMatch permanent ^(.*)$ http://www.domain.com
You can also use one of these in your .htaccess file:
redirect 301 /index.html http://www.domain.com/index.html
redirect permanent /index.html http://www.domain.com/index.html
redirectpermanent /index.html http://www.domain.com/index.html
This will redirect "index.html" to another domain using a
301-Moved permanently redirect.
2. If you need to redirect http://mysite.com to
http://www.mysite.com and you've got mod_rewrite enabled on
your server you can put this in your .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]
or this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
Tip: Use your full URL (ie http://www.domain.com) when
obtaining incoming links to your site. Also use your full
URL for the internal linking of your site.
3. If you want to redirect your .htm pages to .php pages
andd you've got mod_rewrite enabled on your server you can
put this in your .htaccess file:
RewriteEngine on
RewriteBase /
RewriteRule (.*).htm$ /$1.php
4. If you wish to redirect your .html or .htm pages to
.shtml pages because you are using Server Side Includes
(SSI) add this code to your .htaccess file:
AddType text/html .shtml
AddHandler server-parsed .shtml .html .htm
Options Indexes FollowSymLinks Includes
DirectoryIndex index.shtml index.html
301 ON WINDOWS SERVER:
If you are not using a Unix or Apache server, you can still do a 301 redirect, if you are using .asp pages. Here's what you do:
Place the following code above your tag or , if you have one:
<%@ Language=VBScript %>
<%
response.status="301 moved permanently"
Response.AddHeader "Location", "http://www.domain.com/file-location.html"
%>
<%
response.status="301 moved permanently"
Response.AddHeader "Location", "http://www.domain.com/file-location.html"
%>
If you are using .asp.net pages, the code should look like:
The URL after "Location" should be the exact location to which you want the page to redirect.
SOME REDIRECTS:
ColdFusion Redirect
<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.new-url.com">
<.cfheader name="Location" value="http://www.new-url.com">
PHP Redirect
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>
Header( "Location: http://www.new-url.com" );
?>
ASP Redirect
<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.new-url.com/"
%>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.new-url.com/"
%>
ASP .NET Redirect
JSP (Java) Redirect
<%
response.setStatus(301);
response.setHeader( "Location", "http://www.new-url.com/" );
response.setHeader( "Connection", "close" );
%>
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/");
print $q->redirect("http://www.new-url.com/");
Ruby on Rails Redirect
def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.new-url.com/"
end
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.new-url.com/"
end
- 302 REDIRECT
Here you are telling the search engines to read and use the content on the new page, but to keep checking the original URL first as it will ultimately be reestablished.
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
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
Redirect /directory http://www.domain.com/temporary-directory
Redirecting an entire site
Redirect / http://www.temporary-domain.com/
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();
?>
header(”Location: http://www.domain.com/temporary-address/temporary-file-name.html”);
exit();
?>