SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I use 301 redirect with 404 error in htaccess

    Hi,

    I never hardly have broken links in my pages, but just incase I do I would like it to return as a normal 404 error which is already does.

    But I also have the broken links that are on other sites return with a 404 error. So when someone comes to my site from a link that no longer exists on my site how can I make this become a permanent 301 redirect instead of a 404 error. I still want the pages on my site to return with a 404 error if there is a broken link on my site. But with external broken links to my site I want a 301 redirect.

    I know that I will need to match the referer with my sites domain name / url and if it is true then it will bring a 404 error and if my domain is not the referrer then it would be a 301 redirect instead.

    How can I do this by just using .htaccess and also does others do this and is it safe for the major search engines.

    Thanks and I hope you understand what I mean.

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    j2,

    No, actually, I DON'T know what you mean. If you don't know what file has been requested, it's difficult to know how to redirect it to a permanent link (other than your own 404 page which is NOT a permanent redirect, is it?).

    You COULD use mod_rewrite to redirect anything that does NOT match your directories, your files or your redirects to send to 404 but Apache has the more elegant solution: ErrorDocument 404 /404.php where the first 404 is the error code and /404.php is the ABSOLUTE link to your 404 handler.
    Client Request Redirected
    300 Multiple Choices
    301 Moved Permanently
    302 Moved Temporarily
    303 See Other
    304 Not Modified
    305 Use Proxy
    I don't think that you want anything but a 302 is reasonable until you can identify the 404s from your error log and create a redirection (if applicable).

    Personally, I use the ErrorDocument statement (and redirect to my sitemap). I've had other sites link to my site to pages that simply do not exist (presumably, in an attempt to annoy anyone who would visit my site) but I don't have the time to chase these bogus links - ErrorDocument and a well maintained sitemap take care of that for me.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I wanted is for all my broken internal links to return a 404 error page on the site and then all of the broken external links that people click on from others sites that come to mine I wanted them to become a 301 permanent redirect where they will redirect to the homepage.

    The reasons for this is because I have noticed there are sometimes quite a bit of traffic coming from broken links and I prefer the visitor to go straight to the homepage if it's a broken link and I will probably also benefit from PR gain as it is a 301 permanent redirect.

    Can this be done?

    The referrer thing would need to be used so that it knows whether the visitor has come straight from an external site or an internal site page.

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    j2,

    What you're asking is to use {HTTP_REFERER} to determine whether a 404 came from an internal source or external - obviously NOT a job for ErrorDocument:
    Code:
    # Place AFTER existing mod_rewrite code
    RewriteCond %{HTTP_REFERER} !mysite\.com [NC]
    RewriteCond %{REQUEST_URI} !-d
    RewriteCond %{REQUEST_URI} !-f
    RewriteRule .? index.php [R=301,L]
    If not from mysite.com (No Case) AND
    if not a directory AND
    if not a file
    redirect anything to index.php with a permanent redirect.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi dklynn you are great.

    Would this be search engine friendly or would it be a risk for a penality. I am obviously doing this for my visitors and not for the search engines, but these days you have to make sure that you make your site for your visitors and also keep the search engines in mind so that you don't do anything wrong to upset them either.

    If you think it is ok then I will implement it onto my site asap.

    Thanks!

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    j2,

    The SE's should be happy with the 301 status as it tells them to update their db.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So about 1,000 different urls pointing to the same page will not be a problem. Google will not see it as 1,000 urls with the same content on it meaning duplicate content?

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    j2,

    They SHOULD as it is the same page - that's why 301 is often NOT used. SE's DON'T like to offer the same page for everything so the "User Friendly URLs) are used with redirection (without the 301) to the script which processes the URI data in the query string, i.e., site.com/somepage => site.com/index.php?page=somepage.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't quite understand what you mean. Do you mean that google will see every url that points to 1 page using a 301 redirect as duplicate pages or will they not? If they will then I have used 301 redirect to change http://sitename to http://www.sitename and sitename.com/index.html to http://www.sitename.com etc.

  10. #10
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    j2,

    This is more in stymiee's realm, the Search Engine Optimization forum.

    However, it's my understanding that 301's are good to redirect to an identical "page" on another site or making name or extension changes. However, redirects to a single script should NOT be 301'd as that is generally read as identical content (despite a different query string). Therefore, I see a 301 as useful to redirect to a "temporary" location from which you can redirect to a script to deal with the 404 case.

    Confused? Say that you DON'T have somepage.html on your website (and don't have some mod_rewrite that would do something with that URI). You COULD redirect that to anotherpage.html with a permanent redirect THEN send another.html?badlink=somepage.html to 404.php (which would have the new query string appended).

    somepage.html => anotherpage.html?badlink=somepage.html (permanent)
    anotherpage.html => 404.php
    (?badlink=somepage.html would be automatically appended)

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •