1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

301 Redirect/.htaccess FAQ's

Discussion in 'White Hat SEO' started by barsha, Jun 25, 2008.

  1. barsha

    barsha Registered Member

    Joined:
    Apr 9, 2008
    Messages:
    73
    Likes Received:
    87
    What is a 301 redirect?


    301 Redirect - this tells the SEs that your old site has "permanently moved" to your new site. Your old site will be gradually removed from the SE indexes and your new site will take its place. The advantage of using this approach is that the 301 Redirect will also transfer the link popularity gained by your old site, to the new one. The bad news is Google doesn't seem to recognize that you've simply changed domains and will subject your new domain to the aging delay as if it were a brand new site. The result is that your rankings will likely suffer, especially in more competitive markets. Follow Andy's advice on avoiding Google Sandbox.




    Miroslav Chodak


    All the 30x tell browsers and spiders both to 'do the same thing', but what spiders do with the results are unique to the needs of search engines. Specifically, a 301 will "suggest" to the spider that the original URL should be dropped and replaced with the subsequent one whereas the 302 "suggests" that the redirect is short lived so the original URL should be kept as the preferred means to identify the page.




    Leslie Rohde


    When to use a 301 redirect?


    It is the URL that is important in linking, so you only need a 301 when URLs change. This includes changing url from http://yourdomain.com to http://www.yourdomain.com .




    Leslie Rohde


    How to implement a 301 redirect?


    Implementing 301 redirect is fairly simple. The version to implement is based on your server requirements. See below for details:


    Redirect Old domain to New domain (htaccess redirect)

    Create a .htaccess file with the below code, it will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain.
    The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

    If you want to implement such redirect on your site, add the following to your .htaccess file:

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

    This will redirect all non-www requests to a www version.

    Miroslav Chodak
    The following redirects were referenced from http://www.webconfs.com/how-to-redirect-a-webpage.php . This website was once suggested by Andy, but provided by Aaron Hu:


    IIS Redirect

    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'
    Redirect in ColdFusion
    <.cfheader statuscode="301" statustext="Moved permanently">
    <.cfheader name="Location" value="http://www.new-url.com">

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

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

    Redirect in ASP .NET
    <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>

    Redirect to www (htaccess redirect)
    Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to http://www.domain.com
    The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

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

    Please REPLACE domain.com and http://www.newdomain.com with your actual domain name.

    Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

    If I Put an html Redirect on My Site, Will I Be De-indexed from Google?



    Your old site will eventually be de-indexed from the SE?s. Your new site will not. The safest way to redirect old web pages to the new pages or old web site to the new web site and keep the same search engine rankings is to use the 301 redirect. It will also pass on the page rank from your old site to you new site.

    Problem One: I have a URL that looks like this: http://www.domain.com/cars.php?toyota. I want it to look like this instead: http://www.domain.com/cars/toyota.

    Solution:

    RewriteEngine On
    RewriteRule ^/cars/(.*) cars.php?$1 [PT]

    Why This Works: Anything after /cars/ is removed, stripped and placed as a folder.

    Problem Two: I have a URL that look like this: http://www.domain.com/cgi-bin/product.cgi?bottle=shampoo=oily. I want it to look like this instead: http://www.domain.com/product/shampoo/oily.

    Solution Two:

    RewriteEngine On
    RewriteRule ^/product/([^/]*)/([^/]*)
    /cgi-bin/product.cgi?bottle=$1&topic=$2 [PT]

    Why This Works: It requires that the requested URL look exactly like the URL that is described in the pattern. This allows for a cleaner URL to be spidered easier.

    Problem Three: I used an HTML editor before and it named all of my files .htm but I want to go to .html as a standard on my site. I need the old URLs to work as my site is heavily bookmarked, but I don't want to get hit with the duplicate content penalty.

    Solution Three:

    RewriteEngine On
    RewriteRule (.*).htm $1.html [PT,L]

    Why This Works: The "$1" passes the entire URL back and only the extension is changed. Any query string from a dynamically generated page will be passed as well.

    Problem Four: I want all non-www URLs to go to their www version to protect against duplicate content issues with Google.

    Solution Four:


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


    Um, this should go without saying, but make sure you replace "domain" with your actual domain name or this isn't going to work too well for you!

    If you have sub domains, you want the following code. You only want to use one instance of this code. So either use the one above, or the one below.

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

    Why This Works: Regardless of what the user types or what another site has linked, your home page and any subpage will appear with the "www".

    Problem Five: I just checked my log files and I am getting a ton of websites who are hijacking my images and referring to my site for them, thus using my bandwidth for their theft. What can I do?

    Solution Five: Block the use of your image files directly on the server.

    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://domain.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://www.domain.com.*$ [NC]
    RewriteRule .(gif|jpg|png)$ - [F]

    Why This Works: Any image file (gif, jpg, png) is rewritten to a forbidden status from any outside request, thus protecting your bandwidth. Of course, the dirty way to solve this is to change out the images to something that is a huge file to the thief's website takes forever to load. Jake Baillie made famous swapping the images to porn images, but that also got him into some trouble with some attorneys, so be careful out there.

    Problem Six: I want to be able to redirect the visitors the root of the folder, not to the index.html. So instead of seeing http://www.domain.com/index.html they would see http://www.domain.com.

    Solution Six:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{THE_REQUEST} ^.*/index.html
    RewriteRule ^(.*)index.html$ http://www.domain.com [R=301,L]

    Why This Works: This takes the "index.html" file and rewrites it to the root. If your main file was "index.htm" or "index.php" you would put that in there.

    301 Redirects - How and When
    The 301 Redirect is a "Permanent Redirect" and I never advise doing a 302 "Temporary" redirect, as you can do a meta refresh instead. The best way to understand the process is very much the same as a "change of address card" at the post office. You were living in one place and you are moving to another, so you tell the post office, "this is my old address, and this is the address I am moving to." You do the same exact thing in your.htaccess file when you want to move traffic from one location to another. Here are some examples:

    Example One: I want to move page http://www.domain.com/our-long-privacy-policy-url.html to http://www.domain.com/privacy.html
    Redirect 301 /our-long-privacy-policy-url.html http://www.domain.com/privacy.html
    Notice that I did the "old" without the domain name as it isn't needed, just the part of the URL following the domain. The "to" portion needs the entire URL.

    Example Two: We stopped carrying a certain product, so I want to send the traffic to our sister company's website instead.
    Redirect 301 /discontinued-product.html http://www.sistercompany.com/current-product.html

    Example Three: I want to send my PPC traffic to my site first, do a bounce and then send it to the merchant's site so they can't track that it came from a PPC campaign. First, in your PPC campaign, you send the traffic to /product-ppc-landing-page.html which has the product listed. Once the page has been reviewed by the PPC editors, you can do the redirect.
    Redirect 301 /product-ppc-landing-page.html /product-redirect.html
    Redirect 301 /product-redirect.html http://www.merchantsite.com/product.html?affid=5150

    If you want to test to see if you did the redirect correctly, you can enter in the URL of the "old" address with this redirect checking tool.
    Text Link: http://www.webconfs.com/redirect-check.php
     
    • Thanks Thanks x 8
  2. undeterminederror

    undeterminederror BANNED BANNED

    Joined:
    Mar 31, 2008
    Messages:
    630
    Likes Received:
    457
    i use htaccess to redirect subdomains to categories and this is the code:
    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.mysite.com 
    RewriteCond %{HTTP_HOST} ([^.]+).mysite.com 
    RewriteRule ^(.*)$ http://mysite.com/index.php?category_name=%1 
    but i'd like the browser to keep the url of subdomain typed by the user. and just to show the category, not to redirect to its url. do you know if this is possible?
     
  3. red2004

    red2004 Junior Member

    Joined:
    Aug 17, 2007
    Messages:
    129
    Likes Received:
    28
  4. undeterminederror

    undeterminederror BANNED BANNED

    Joined:
    Mar 31, 2008
    Messages:
    630
    Likes Received:
    457
    try this:
    Code:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.sample.com 
    RewriteRule ^(.*)$ http://sample.com/subfolder [L,R=301]
     
  5. comparco

    comparco Regular Member

    Joined:
    Feb 22, 2008
    Messages:
    291
    Likes Received:
    265
    Occupation:
    trying to make a living online
    Location:
    Scotland
    Thank you barsha. My head hurts tho'

    I use 301 to redirect to my affiliate links, so that the link on the page looks like other.html
    when hovered over, and the 301 written on .htaccess looks like
    Redirect 301 /other.html http://myaffiliatelink.html

    am I doing myself harm as far as serp's and ranking are concerned

    kind regards
     
  6. allyrob

    allyrob Newbie

    Joined:
    May 11, 2008
    Messages:
    15
    Likes Received:
    14
    Location:
    Heaven
    Last edited: Jul 16, 2008
  7. undeterminederror

    undeterminederror BANNED BANNED

    Joined:
    Mar 31, 2008
    Messages:
    630
    Likes Received:
    457
    askapache.com/htaccess/301-redirect-with-mod_rewrite-or-redirectmatch.html
     
  8. Bugalugs

    Bugalugs Regular Member

    Joined:
    Jan 27, 2007
    Messages:
    204
    Likes Received:
    381
    I have several domains with the different TLDs...that is same name ....but dot.com, dot net dot org etc. Can I use the 301 redirect on these to the website that is up?
    Don't totally understand all this.

    Rickrufus
     
  9. allyrob

    allyrob Newbie

    Joined:
    May 11, 2008
    Messages:
    15
    Likes Received:
    14
    Location:
    Heaven
    thanks man but i am a noob to it so can you please give me a bit more detail, like where to put in the sites in the code or probably how the code would look with my sites in it.
     
  10. undeterminederror

    undeterminederror BANNED BANNED

    Joined:
    Mar 31, 2008
    Messages:
    630
    Likes Received:
    457
    open your site with a ftp ; create a file named " .htaccess " ; insert this code :

    Code:
    RewriteEngine On
    Redirect 301 / http://www.yoursite.com/subdir/index.html
     
  11. Bugalugs

    Bugalugs Regular Member

    Joined:
    Jan 27, 2007
    Messages:
    204
    Likes Received:
    381
    Could someone please answer my question above even if it is to tell me how dumb I am !
    I am very technically challenged .
     
  12. undeterminederror

    undeterminederror BANNED BANNED

    Joined:
    Mar 31, 2008
    Messages:
    630
    Likes Received:
    457
    if you have the site on .com , then in .net and .org make just a file named ' .htaccess ' and write this 2 lines in it:
    Code:
    RewriteEngine On
    Redirect 301 / http://www.yourmainsite.com/
     
    • Thanks Thanks x 1
  13. Bugalugs

    Bugalugs Regular Member

    Joined:
    Jan 27, 2007
    Messages:
    204
    Likes Received:
    381
    Really am very grateful for this undeterminederror.

    Rickrufus
     
  14. dwpg002

    dwpg002 Senior Member

    Joined:
    Dec 29, 2008
    Messages:
    916
    Likes Received:
    47
    I have web site which has 10K pages index by Google. Now I want to redirect it money site using 301 redirect.

    http://xxx.com ---> http://yyy.com

    301 redirect is working fine if i reach the homepage, for other page's i am getting 404 error. So i want to know is there any way i can redirect the whole web site to http://yyy.com/