Knowledge Essentials - 3Essentials Hosting

301 redirect

Article ID: 411

 Back to Search

Question: How can I implement a 301 Redirect ("permanent redirect") on my site?  For example, I want to redirect all users who come to www.domain.com to go to domain.com...

Answer: There are several ways to implement a 301 redirect, as described below:

Please note that 3Essentials is NOT responsible for the results achieved with any of these code samples, to either site performance or search engine ranking or placement.  You are STRONGLY advised to fully research and understand any functions you implement in your site as well as the impact to your site function and search engine ranking and placement.  Use this code at your own risk.  

Here's also an excellent reference on this topic: http://www.webconfs.com/how-to-redirect-a-webpage.php

HTML Redirection

You can do a redirect with the html meta tag, but we would advise you not to due to it's misuse, most search engines will de-index a page with this usage.  Use one of the solutions noted below.

301 Redirect Using htaccess (for linux hosting customers only)

Using htaccess to accomplish the 301 redirect is highly suggested due to it being fairly convenient to manage, rather than setting redirects on each individual page, you can simply add the redirect code to the .htaccess file.

Here is how to do it:

  1. Create a file on the root directory of your website, name it ".htaccess".
  2. Open the .htaccess file using notepad or what ever text editor that you prefer.
  3. Add this into the .htaccess file, save it and then upload it to your web server:

Redirect 301 /old/old.html http://www.you.com/new.html

NOTE: Don't add "http://www" to the first part of the statement - place the path from the top level of your site to the page. Also ensure that you leave a single space between these elements:

redirect 301 (the instruction that the page has moved)
/old/old.html (the original folder path and file name)
http://www.domain.com/new.html (new path and file name)

301 Redirect Using Mod_Rewrite (for linux hosting customers only)

Mod_Rewrite has got to be one of the most useful modules a server can have in terms of SEO, it allows you to organize the file structure of your web site in a dynamic yet simple fashion. In this example I show a useful method of 301 redirecting with mod_rewrite.

When somebody links to your website, sometimes they dont always link to you in the way that you want them to. If somebody links to www.yoursite.com and somebody else links to yoursite.com, Google will assign a separate pagerank for each of those. By inserting the below example into your .htaccess file, it will solve the problem by redirecting anything linking to domain.com to www.domain.com, also redirecting the pagerank.

RewriteEngine On
rewritecond %{http_host} ^yoursite.com

rewriteRule ^(.*) http://www.yoursite.com/$1 [R=301,L]

If you want, instead, to remove www from a URL, you can use the code below.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.yoursite.com$ [NC]
RewriteRule ^(.*)$ http://yoursite.com/$1 [R=301,L]

 

301 Redirect on a DNN site where you want to redirect traffic from a prior page which no longer exists

(provided by customer Tom)

Set the old page up in the dnn portal. Do Not include it on the menu. In the page management, in the Link section you can link to the new page and select the check box that says Permanent Redirect (this is the DNN version of 301).

301 Redirect with IIS7's Rewrite Module (for Windows/IIS7 based hosting only)

See the Enforce a Primary Hostname configuration that 3Essentials can implement for you, described in this article: http://knowledge.3essentials.com/web-hosting/article/1256/URL-Rewrite-mod_rewrite-for-Windows-on-IIS-7.x.html

 301 Redirect Using PHP

Simply add this code to your page or script:

  • <?
  • header( "HTTP/1.1 301 Moved Permanently" );
  • header( "Status: 301 Moved Permanently" );
  • header( "Location: http://www.new-url.com/" );
  • exit(0); // This is Optional but suggested, to avoid any accidental output
  • ?>

  

301 Redirect Using ASP

Simply add this code to your page or script, within the script tags:

  • <%@ Language=VBScript %>
  • <%
  • Response.Status="301 Moved Permanently"
  • Response.AddHeader "Location", http://www.new-url.com/
  • %>

  

301 Redirect Using ASP .NET

Simply add this code to your page or script: 

  • <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>

 

 
Downloads Associated With This Article
No downloads are currently associated with this article.