Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2009
    Posts
    52

    How to generate error 404 with .htaccess

    hello

    I need to generate a error 404 for all url of my website who contain a specific attribute inside.

    In fact I have many url who contain the attribute ?fontstyle=f-larger AND ?fontstyle=f-smaller and I would like find a solution for let me generate 404 error when trying to access to this kind of url.

    If is not possible maybe I can redirect to a fake/inexistent url like www.site.com/fake.html who will result in a error 404...but I did not see how to do using .htaccess...

    any clue please ??

    thank

  2. #2
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,840
    Something like this should give you a start:
    Code:
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{QUERY_STRING} fontstyle=f-larger
        RewriteCond %{QUERY_STRING} fontstyle=f-smaller
        RewriteRule .* - [G,L]
    </IfModule>
    The [G] flag will give you a GONE (HTTP 410) error. I don't think there's a way to generate a 404 directly but as you say if you rewrite to a non-existent file instead that should work.

    If you do that though, watch out for any other rewrite rules that may catch the missing file request and redirect / rewrite it again (eg. the standard rules on most CMSs for SEO-friendly links that send everything to index.php).
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  3. #3
    Join Date
    Mar 2009
    Posts
    52
    many thank for your reply...

    Yes typically I use Joomla with SEF enabled that mean I have my .htaccess enabled with some standard mandatory rules active inside (related like you say for SEO-friendly links) and with also many more custom security rules.

    I tried your suggested rule but this seem to not work..

    Is because your rules must be place in a specific position or because possible conflict with others rules ?

  4. #4
    ErrorDocument 404 /notfound.html

    P.S: IE will redirect your error page to Bing if your custom error page is smaller than 512 byte.
    - I am a PHP developer. I'm not a webhosting owner, I have never run a webhosting business before.
    - English is not my primary language.

  5. #5
    Join Date
    Mar 2009
    Posts
    52
    Quote Originally Posted by paraiba View Post
    ErrorDocument 404 /notfound.html

    P.S: IE will redirect your error page to Bing if your custom error page is smaller than 512 byte.
    Where I'm supposed to add your code ??

    thank

  6. #6
    You ought to add those codes into the your .htaccess file.
    Hope this helps
    - I am a PHP developer. I'm not a webhosting owner, I have never run a webhosting business before.
    - English is not my primary language.

  7. #7
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,840
    ErrorDocument 404 will not do what you want - it just controls the page shown after you get a 404 error, it won't generate one.
    Quote Originally Posted by dotcom22 View Post
    I tried your suggested rule but this seem to not work..

    Is because your rules must be place in a specific position or because possible conflict with others rules ?
    Most of the standard rewrite rules include [QSA] (query-string append) so your condition ("?fontstyle=f-larger AND ?fontstyle=f-smaller") should persist even after any earlier rewrite, but it's probably worth trying this at the beginning of the top-level .htaccess so it gets processed first. If it still doesn't work, what exactly is the url you're trying to match? (Blank out the domain name if you like). If the url is malformed then perhaps the QUERY_STRING variable as seen by Apache won't include both those parameters.
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  8. #8
    Join Date
    Mar 2009
    Posts
    52
    Quote Originally Posted by foobic View Post
    ErrorDocument 404 will not do what you want - it just controls the page shown after you get a 404 error, it won't generate one.
    Yes I thought this suggestion wont make any effect...

    Quote Originally Posted by foobic View Post
    Most of the standard rewrite rules include [QSA] (query-string append) so your condition ("?fontstyle=f-larger AND ?fontstyle=f-smaller") should persist even after any earlier rewrite, but it's probably worth trying this at the beginning of the top-level .htaccess so it gets processed first. If it still doesn't work, what exactly is the url you're trying to match? (Blank out the domain name if you like). If the url is malformed then perhaps the QUERY_STRING variable as seen by Apache won't include both those parameters.
    I tried to put your rule on the first position but this change nothing...

    My url look like this:

    http://www.site.com/de/category/fiel...ge:2/limit:10/

    Sometime the attribute "fontstyle-f-larger" or "fontstyle-f-smaller" is placed in middle, start or end url...depend.

    Before I had the attribute "?fontstyle-f-smaller" always at the end of url but now it seem the fact to have make some task inside Google Webmaster Tools help to remove those url from Google index...but I'm unsure if this is the real reason.

    any clue ?

    many thank for your support

    cheers

  9. #9
    By creating 404 error for all URLs, will not be bad from web promotion point of view.

  10. #10
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,840
    Code:
    http://www.site.com/de/category/fieldname1/fieldname2/menu/fontstyle-f-larger/page-2/limit-5/page:2/limit:10/
    Ok, so first that url doesn't contain a query string of any kind (eg. ?fontstyle=f-larger, as stated in the OP) and second it doesn't contain both "?fontstyle=f-larger AND ?fontstyle=f-smaller". So, what exactly is the condition you want to use to trigger a 404 error?
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  11. #11
    Join Date
    Mar 2009
    Posts
    52
    Sorry I was maybe not clear... My english is poor and sometime I'm not able to well explain everything.

    In fact I would like generate this 404 error only when the following exact attribute is present in the rewritted url:


    fontstyle=f-larger

    OR

    fontstyle=f-smaller

    No matter if the attribute is rewritted by the sef engine or if other character are present before or after the attribute (like a backslash or other).

    Now I don't know if this is possible to do..

    thank

  12. #12
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,840
    Yes, it's possible. You could use the REQUEST_URI server variable instead of QUERY_STRING and the [OR] flag to redirect on either condition. But again, the details matter. Is it "fontstyle=f-larger" or "fontstyle-f-larger" as shown in your example url? Assuming the latter, this should do it:
    Code:
    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_URI} fontstyle-f-larger [OR]
        RewriteCond %{REQUEST_URI} fontstyle-f-smaller
        RewriteRule .* - [G,L]
    </IfModule>
    One other thought: If your own website is generating these "bad" urls (perhaps in response to an unusual series of clicks by the visitor) then you need to fix that, not just send out error pages when an unsuspecting visitor clicks on one of your links.
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  13. #13
    Join Date
    Mar 2009
    Posts
    52
    excellent your code now work and if I try to access to an url who contain the attribute I get this message:

    *******************
    Gone
    The requested resource
    is no longer available on this server and there is no forwarding address. Please remove all references to this resource.

    Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
    ******************

    But now I get also a error 403 and I don't know if this is a good thing.

    In fact like you have probably understand, I try to remove all url of my site who contain the attribute fontstyle=f-larger OR fontstyle=f-smaller because those url was a case of duplicate content and Google don't like this.

    MY Joomla template had 2 button who allow to increase or decrease the font size in all page of my site and give this choice to visitors. That mean my site is more less indexed 3 time by google:

    1) With normal font size
    2) With big font size
    3) With small font size

    After discovering this problem I removed those "font size button" from my template, but is now too late because all my pages are present in google index.

    For informations here a list of common error about HTTP status codes with some google comment:

    http://support.google.com/webmasters...n&answer=40132

    If you read about error 410, google suggest to use also an error 301 an redirect to another page but in my case this is not possible because I don't have any page to redirect and my goal was to get an error 404.

    Ideally a true error 404 would be better for allow google understand this url is really no more available and need to be removed from his index. Now I'm not sure if google will consider an error 410 or error 403 like an error 404.

    Maybe it would be better to use a error 301 instead to error 410 and redirect to a fake inexistent page who will produce a true error 404 ???

    have you any advices about this ??

    thank again

    cheers

  14. Newsletters

    Subscribe Now & Get The WHT Quick Start Guide!

Similar Threads

  1. .htaccess, indices, & 404 error
    By expatCanuck in forum Hosting Security and Technology
    Replies: 4
    Last Post: 06-07-2009, 04:44 PM
  2. .htaccess Custom 404 Error Page returning 200 code
    By Lanny in forum Hosting Security and Technology
    Replies: 10
    Last Post: 08-09-2007, 07:42 PM
  3. using htaccess for 404 error trapping
    By sametch in forum Dedicated Server
    Replies: 10
    Last Post: 01-24-2003, 03:01 AM
  4. .htaccess 404 error message
    By Ellmb in forum Hosting Security and Technology
    Replies: 3
    Last Post: 03-05-2001, 01:17 PM
  5. .htaccess and 404 error page
    By astralexis in forum Hosting Security and Technology
    Replies: 2
    Last Post: 12-28-2000, 03:56 AM

Related Posts from theWHIR.com

Posting Permissions

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