homepage Welcome to WebmasterWorld Guest from 54.198.42.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
mod rewrite: 500 error if user enters invalid characters in URL
master w bates III




msg:3584660
 4:55 am on Feb 26, 2008 (gmt 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,
test/index.php

$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...";
}

.htaccess

DirectoryIndex index.php
ErrorDocument 404 errors/404.php

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

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 127.0.0.1 Port 80

error_log

[Mon Feb 25 20:48:59 2008] [error] [client 127.0.0.1] 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?

 

jdMorgan




msg:3584674
 5:22 am on Feb 26, 2008 (gmt 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! ;)

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved