hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : How to generate error 404 with .htaccess
Reply

Forum Jump

How to generate error 404 with .htaccess

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old
Junior Guru Wannabe
 
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



Sponsored Links
  #2  
Old
Community Liaison 2.0
 
Join Date: Feb 2005
Location: Australia
Posts: 5,825
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  
Old
Junior Guru Wannabe
 
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 ?

Sponsored Links
  #4  
Old
WHT Addict
 
Join Date: Sep 2011
Posts: 127
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  
Old
Junior Guru Wannabe
 
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  
Old
WHT Addict
 
Join Date: Sep 2011
Posts: 127
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  
Old
Community Liaison 2.0
 
Join Date: Feb 2005
Location: Australia
Posts: 5,825
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  
Old
Junior Guru Wannabe
 
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  
Old
New Menber
 
Join Date: Apr 2012
Posts: 3
By creating 404 error for all URLs, will not be bad from web promotion point of view.

  #10  
Old
Community Liaison 2.0
 
Join Date: Feb 2005
Location: Australia
Posts: 5,825
Quote:
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  
Old
Junior Guru Wannabe
 
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  
Old
Community Liaison 2.0
 
Join Date: Feb 2005
Location: Australia
Posts: 5,825
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  
Old
Junior Guru Wannabe
 
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

Reply

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

Related posts from TheWhir.com
Title Type Date Posted
In an Attempt to Combat Malware, Microsoft Brings Down Good and Bad Customers of DNS Service No-IP Web Hosting News 2014-07-03 12:24:18
Admin Error Brings Down Joyent’s Ashburn Data Center Web Hosting News 2014-05-28 10:18:18
Unusual DNS Error Leads to Internet Outage in China Web Hosting News 2014-01-21 12:16:24
Lengthy Outages for Hacker News, FastHosts Web Hosting News 2014-01-06 09:56:36
Network Issues Cause Amazon Cloud Outage Web Hosting News 2013-09-16 12:18:53


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
WHT Host Brief Email:

We respect your privacy. We will never sell, rent, or give away your address to any outside party, ever.

Advertisement:
Web Hosting News:
WHT Membership
WHT Membership



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?