Forum Moderators: phranque

Message Too Old, No Replies

rewrite if uri match

         

papajay

10:01 am on Dec 3, 2009 (gmt 0)

10+ Year Member



Hello,

I've problem with mod rewrite "again"...
I need to prevent access to url without query, which return 404 header from noexist.php

<?php
header("HTTP/1.0 404 Not Found");
?>

example:

blabla.php should return 404 / not found
blabla.php?query=whatever should return 200 / success

I've tried this:

RewriteRule ^blabla\.php$ noexist.php [NC]

Both url "success" returning 404 / not found

Any suggestion?

g1smd

12:36 pm on Dec 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need a RewriteCond before the RewriteRule.

That RewriteCond should check to see if QUERY_STRING is blank.

Alternatively, your script could be programmed to directly return a 404 HEADER and a suitable error message if the parameters were blank or missing.

.

Your script should also return a 404 status code if any query string value is non-valid.

papajay

2:42 pm on Dec 3, 2009 (gmt 0)

10+ Year Member



Thanks g1smd,
btw I'm new on php, I will try to alter the script, seems it should be done in that way than with mod rewrite.

jdMorgan

3:48 pm on Dec 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, because only the script itself can tell whether or not it can generate valid content for a given URL+query-string value. Therefore the script itself must return a 404 status and the text contents of your 404 error page.

Jim

papajay

5:34 pm on Dec 3, 2009 (gmt 0)

10+ Year Member



Thanks Morgan, here is my quick and dirty way :)
No checking on query, just the request uri.


<?php
function errPageURL() {
if (preg_match('/blabla.php$¦otherblabla.php$/',$_SERVER['REQUEST_URI'],$matches)) {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
exit;
}
return true;
}
?>
<?php
errPageURL()
?>

jdMorgan

5:49 pm on Dec 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to output (or include) some HTML text to make up the body of the 404 error page, there...

If *all* queries to 'blahblah.php' should result in a 404, then you don't need the script and can do this in .htaccess as gismd describes above. But if any queries are valid, then you need your script to take the query parameters, check your database with them, and decide if it can return a valid 'page' of content.

Jim