#1
  1. Busting SEOChat Spammers!
    SEO Chat High Scholar (3500 - 3999 posts)

    Join Date
    Jul 2009
    Location
    UK - Kent - Maidstone - PC!
    Posts
    3,754
    Rep Power
    1474

    .htaccess canonical 301 redirects


    I have noticed and lot of people asking for help with canonical urls, canonical 301s and .htaccess redirects in general. So I thought I would make a lovely thread for us all to point people to that need this information

    First of all before editing your .htaccess file you should always take a copy of your current one in case something goes wrong.

    www to non-www 301 redirect:
    Code:
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^domain\.com$
    RewriteRule (.*) http://domain.com/$1 [R=301,L]
    non-www to www 301 redirect:
    Code:
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.domain\.com$
    RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
    index.php to domain.com/ redirect (change extension as needed):
    Code:
    Options +FollowSymLinks
    RewriteEngine on
    # index.php to /
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
    RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
    301 reidrect old page (change extension as needed):
    Code:
    Redirect 301 /old-page.html http://www.domain.com/page.php
    Change file extension (change extension as needed):
    Code:
    RedirectMatch 301 (.*)\.html$ http://www.domain.com$1.php
    A couple of bits to remember:
    Code:
    Options +FollowSymLinks
    RewriteEngine on
    1. The above should only be used once in a .htaccess file and always be before your reidrect code as above examples
    2. .htaccess will not work on windows, its only for linux.
    3. upload .htaccess files in ascii mode, sending it up as binary will break it.
    4. Be sure to check for a current .htaccess file, as "." files are hidden so check your settings to make sure you can see them.
    5. Remember to change domain.com to your domain!
    6. Last of all, always check your changes to make sure its working correctly.

    There are other ways of doing 301 reidrects so if you cant use .htaccess have a search on here and google and Im sure you will find what you need, if not simple ask

    Comments on this post

    • LazyBum agrees
    • jsteele823 agrees
    Last edited by NathanielB; May 17th, 2012 at 04:39 AM.
    New To SEOChat? See Here & the FAQ's

    "stupid people say stupid things, and smart people out smart each other!" SOAD
    301 Redirects | SEO Tools | Default htaccess | CuteRank
  2. #2
  3. Busting SEOChat Spammers!
    SEO Chat High Scholar (3500 - 3999 posts)

    Join Date
    Jul 2009
    Location
    UK - Kent - Maidstone - PC!
    Posts
    3,754
    Rep Power
    1474
    I forgot to post the code for redirect http to https Not that meany need it but its always nice to have a reference IMO

    Code:
    RewriteCond %{HTTP_HOST} ^www.site.co.uk$
    RewriteRule ^/?$ "https\:\/\/site\.co\.uk" [R=301,L]
    Also if I have missed any more out that people would like adding just let me know via PM so we can keep the thread clean, or simple add it yourself but be sure its right and that you dont post whats already here

    Comments on this post

    • KarnRawat agrees : please tell me how to fix cannonical issue for ****.blogspot.com
  4. #3
  5. No Profile Picture
    Registered User
    SEO Chat Explorer (0 - 99 posts)

    Join Date
    May 2011
    Posts
    4
    Rep Power
    0
    Originally Posted by NathanielB
    I forgot to post the code for redirect http to https Not that meany need it but its always nice to have a reference IMO

    Code:
    RewriteCond %{HTTP_HOST} ^www.site.co.uk$
    RewriteRule ^/?$ "https\:\/\/site\.co\.uk" [R=301,L]
    Also if I have missed any more out that people would like adding just let me know via PM so we can keep the thread clean, or simple add it yourself but be sure its right and that you dont post whats already here
    Here i found some useful .htaccess code for redirection methods. Thanks for sharing. It will be helpful for SEO newbies like me. Once again thank u..
  6. #4
  7. No Profile Picture
    Registered User
    SEO Chat Explorer (0 - 99 posts)

    Join Date
    Jan 2011
    Location
    London
    Posts
    9
    Rep Power
    0
    Thanks this is useful! If you go to Matt Cutts blog there's a recent post about rel=canonical vs. 301s, just a few days old.
  8. #5
  9. Busting SEOChat Spammers!
    SEO Chat High Scholar (3500 - 3999 posts)

    Join Date
    Jul 2009
    Location
    UK - Kent - Maidstone - PC!
    Posts
    3,754
    Rep Power
    1474
    Ok so I have been working on a standard .htaccess file which I can simple slap on all my new sites and client sites without having to make any changes or remember whats doing what! And here it is for all to enjoy

    Code:
    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
    #
    # Rewrite .php to extensionless URL:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    #
    # Externally redirect requests for .php URLs to extensionless URLs:
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.domain.com/$1 [R=301,L]
    #Above line has to use your url not HTTP_HOST as that dont work!
    Last edited by NathanielB; Aug 25th, 2011 at 10:10 AM.
  10. #6
  11. Super Moderator
    SEO Chat Genius (4000 - 4499 posts)

    Join Date
    May 2007
    Posts
    4,494
    Rep Power
    1898
    Don't forget - not all hosts are configured alike.

    If you run into 500 errors (internal server), then you'll most likely need to remove the "RewriteEngine On" (if I remember correctly).

    Making a backup of the original before you make changes is excellent advice.
  12. #7
  13. No Profile Picture
    Registered User
    SEO Chat Explorer (0 - 99 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Https redirect query


    Hi,
    We want to setup a 301 redirect from non www. to www.
    If I do this will it affect the https urls?

    Cheers
    LDM
  14. #8
  15. Busting SEOChat Spammers!
    SEO Chat High Scholar (3500 - 3999 posts)

    Join Date
    Jul 2009
    Location
    UK - Kent - Maidstone - PC!
    Posts
    3,754
    Rep Power
    1474
    Originally Posted by LighthouseDM
    Hi,
    We want to setup a 301 redirect from non www. to www.
    If I do this will it affect the https urls?

    Cheers
    LDM
    I should redirect the non-www to the www for both http and https (if not duplicate the code and make one copy use http and the second copy use https), but if you want to redirect http to https, then no you have to use the following to redirect:

    Code:
    RewriteCond %{HTTP_HOST} ^www.site.co.uk$
    RewriteRule ^/?$ "https\:\/\/site\.co\.uk" [R=301,L]
    Oh and please don't try the fake sig again because it will just be removed and you will end up with a ban if you keep it up!

Similar Threads

  1. .htaccess files
    By beachbum in forum SEO Help (General Chat)
    Replies: 42
    Last Post: Apr 12th, 2009, 12:21 PM
  2. 301, 302, 303 and 307 redirects
    By yintercept in forum Google Optimization
    Replies: 10
    Last Post: Aug 24th, 2007, 05:28 PM
  3. 301 redirects using .htaccess
    By irish_in_canada in forum Web Design, Coding and Programming
    Replies: 6
    Last Post: Feb 3rd, 2007, 02:51 AM
  4. 301 Redirects and .htaccess
    By Bazza UK in forum SEO Help (General Chat)
    Replies: 0
    Last Post: Dec 12th, 2005, 09:28 AM
  5. Somebody define a canonical link?
    By crxvfr in forum Google Optimization
    Replies: 10
    Last Post: Oct 21st, 2005, 02:49 PM

IMN logo majestic logo threadwatch logo seochat tools logo