Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod rewrite: 500 error if user enters invalid characters in URL

4:55 am on Feb 26, 2008 (gmt 0)

New User

10+ Year Member

joined:Nov 5, 2007
votes: 0

I am having a problem getting this working. Essentially what I want to do is throw a 404 anytime someone enters an invalid URL.

I am using mod_rewrite on all queries on my site. I am trying to secure it so that anytime a url that is not found in the database it throws a 404.

The problem I am having is mod_rewrite gives me a 500 error if special characters are entered. I am only using lowercase letters, numbers and hyphens in all URLs on my site.

Here is the break down,

$url = $_GET['p'];
$url2 = strtolower(preg_replace('/[^a-z0-9-]/i','',$url));;

if ($url != $url2) {
echo "404";
else {
echo "url is fine, fetching from database...";


DirectoryIndex index.php
ErrorDocument 404 errors/404.php

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([a-z0-9-]+)$ index.php?p=$1 [L]

this is the error i get in the browser if i enter test!
this works file with test or te-st

Not Found

The requested URL /test/scratch/test! was not found on this server.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

Apache/1.3.33 Server at Port 80


[Mon Feb 25 20:48:59 2008] [error] [client] Invalid error redirection directive: errors/404.php

how can i just have the custom 404 page displayed if the user enters any characters that should not be in the url at all?

5:22 am on Feb 26, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
votes: 0

The error message is pretty clear -- Your ErrorDocument syntax is incorrect. In addition, you've got unnecessary characters in your pattern, and using <IfModule> as you've done will lead to silent failures if this code is ported to a server where mod_rewrite is not installed. As such, it benefits you little. Id' suggest:

DirectoryIndex index.php
ErrorDocument 404 [b]/er[/b]rors/404.php
RewriteEngine on
RewriteRule ^([b][a-z0-9\-][/b]+)$ [b]/in[/b]dex.php?p=$1 [L]

When you've got an error message pointing to a specific directive, look up that directive in the Apache documentation and examine the differences between your code and the example code. If there is an inexplicable difference, then it's likely your code is incorrect. This is server configuration -- Wrong or right, every single character counts! ;)