views:

25

answers:

2

I have this in my htaccess:

Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteRule ^annons/([a-zA-Z0-9_]+)$ ad.php?ad_id=$1 [NC]
ErrorDocument 404 /404.html

This all works, except for one problem.

When I write an adress that doesn't exist like this:

   http://www.domain.com/some_adress_that_doesnt_exist

then the 404.html is displayed.

BUT, if I write it like this:

   http://www.domain.com/annons/some_adress_that_doesnt_exist

Notice the /annons/ doesn't actually exist, it is "created" in the rewrite-rules...

It is ONLY when I write the /annons/ and then a url that doesn't exist that the 404 doesn't work. It works with all other subdirectories and combinations, but I am guessing my rewriterule has some flaws...

Any ideas?

Thanks

BTW: By not working I mean a blank white page shows up with no information in it, as if the page existed but was completely blank.

+1  A: 

It is ONLY when I write the /annons/ and then a url that doesn't exist that the 404 doesn't work.

How is the server supposed to know which /annons/ exist and which ones don't? You are always redirecting to ad.php which does exist. No 404 happens here as far as the server is concerned.

The case of a non-existant ad is something you will need to deal with inside your script, e.g. by throwing a HTTP/1.0 404 Not Found header and showing an error page. (or, better, use the SERVER_PROTOCOL variable as @Gumbo demonstrates)

Pekka
Just so you know, the page that shows (blank white page) has no source it seems... When i click "view source" it is empty... Can I be sure that this is in fact the ad.php page?
Camran
@Camran probably. You'll have to output something on the ad.php page to find out for sure.
Pekka
+1  A: 

Well, since such URLs are getting rewritten to an actually existing file (i.e. ad.php), the requested file was found.

Now if your ad.php script decides that the requested resource does not exist, you need to handle that with PHP by responding with a 404 status code, e.g.:

header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found');

After that you then could return the error document to get the same result:

readfile('404.html');
Gumbo
Do I do this in an if statement? For example, if (!isset($_GET[id])) ?
Camran
@Camran you are bound to have some logic in your ad script that loads the specified ad, and fails if it finds none. That is the place to react, isn't it?
Pekka
@Camran: You probably have some kind of database where all valid IDs are. At some point there probably is a test if such an ID exists; and if it fails, your *ad.php* script is probably doing nothing.
Gumbo