Page 1 of 1

404 Redirect as mod_rewrite substitute

Posted: Wed May 28, 2008 7:50 am
by Verminox
Everybody knows how mod_rewrite can make URLs Search Engine Friendly + Readable + Nice :)

But we don't always have hosts who have mod_rewrite enabled, and some might even deny our humble request. What do we do then? I looked around (mostly Google Search) and all the alternatives seem to require your links in the fashion: http://example.com/index.php/something/like/this and then splitting up $_SERVER['REQUEST_URI'] in index.php.

While this works, I don't like the index.php in the URL :x

So then I got an idea, maybe this has been done before, but I couldn't find it being implemented anywhere. Why can't we just set up a custom error handler in .htaccess with ErrorDocument (this functionality is usually more available than RewriteRule) to handle 404 errors and redirect them to a PHP page which can then break up the URL to give some meaningful output.

The only fault in this I see is that although the responseText will contain the expected page, the HTTP Status will be 404, which is bad web design. But can't we just change the response code using PHP's header() function and make it work like a normal page?

I'm not sure whether this idea is good, bad design, infeasable or just stupid. Comments are appreciated :)

Re: 404 Redirect as mod_rewrite substitute

Posted: Wed May 28, 2008 8:15 am
by VladSun

Code: Select all

RewriteEngine   On
RewriteCond     %{REQUEST_FILENAME} !-f
RewriteCond     %{REQUEST_FILENAME} !-d
RewriteRule     ^(.*)$ /index.php?/$1 
 
No index.php in the URL required ...

Re: 404 Redirect as mod_rewrite substitute

Posted: Wed May 28, 2008 8:32 am
by Verminox
The point was to use redirects when mod_rewrite is not enabled.

Re: 404 Redirect as mod_rewrite substitute

Posted: Tue Jun 03, 2008 8:17 pm
by Ambush Commander
404 handlers are actually quite nice black wizardry, but it depends on your host letting you use PHP files for 404s (if they're not letting use .htaccess, it's unlikely they're letting you change the 404 handler).

Yes, you can change the HTTP result header (test it out yourself with Tamper Data on Firefox, or something). It will work like a normal page.

The only problem is it's black wizardry. 8)

Re: 404 Redirect as mod_rewrite substitute

Posted: Fri Jun 06, 2008 11:34 am
by Verminox
Ambush Commander wrote:404 handlers are actually quite nice black wizardry, but it depends on your host letting you use PHP files for 404s (if they're not letting use .htaccess, it's unlikely they're letting you change the 404 handler).

Yes, you can change the HTTP result header (test it out yourself with Tamper Data on Firefox, or something). It will work like a normal page.

The only problem is it's black wizardry. 8)

Maybe the host is letting you use .htaccess but they just don't have mod_rewrite on Apache. Hmm?

Re: 404 Redirect as mod_rewrite substitute

Posted: Fri Jun 06, 2008 2:38 pm
by pickle
Isn't the 404 header sent by Apache already? Would a 200 header following that be ignored, or take precedence?

Re: 404 Redirect as mod_rewrite substitute

Posted: Sat Jun 07, 2008 12:20 am
by Verminox
pickle wrote:Isn't the 404 header sent by Apache already? Would a 200 header following that be ignored, or take precedence?
I think if you add a PHP handler and then call the header() function in it, it will overwrite any previous headers of the same name, in this case the status code.

Re: 404 Redirect as mod_rewrite substitute

Posted: Sun Jun 08, 2008 6:06 am
by Verminox
Yup, I tested it out and it works fine...

.htaccess

Code: Select all

ErrorDocument 404 /dev/rewrite/rewriter.php
rewriter.php

Code: Select all

<?php
switch($_SERVER['REQUEST_URI'])
{
    case '/dev/rewrite/foo':        
        header('HTTP/1.x 200 OK');
        echo 'foo';
        break;
    default:
        header('HTTP/1.x 404 Not Found');
        echo 'Page cannot be found';
}
?>

Test Case 1
Request: http://localhost/dev/rewrite/foo
Response Status: 200
Response Text: foo

Test Case 2
Request: http://localhost/dev/rewrite/xyz
Response Status: 404
Response Text: Page cannot be found

Re: 404 Redirect as mod_rewrite substitute

Posted: Sun Jun 08, 2008 6:23 am
by onion2k
Won't that do all sorts of horrible things to your log files?

Re: 404 Redirect as mod_rewrite substitute

Posted: Sun Jun 08, 2008 7:34 am
by Eran
Yeah, sounds like the error log file will grow to enormous proportions if every request issues a 404.

Re: 404 Redirect as mod_rewrite substitute

Posted: Sun Jun 08, 2008 11:45 am
by Ambush Commander
I've found that, due to spambots and the like, that error logs aren't very useful. I never look at them. If you want to exclude 404s from the log, however, you can always setup conditional logging with Apache, and log any "real" 404s via PHP.

Re: 404 Redirect as mod_rewrite substitute

Posted: Sun Jun 08, 2008 12:01 pm
by Eran
I always use the server error logs for quick debugging on production machines (which are configured with error_reporting = off). You could configure apache to not log 404's - however, if you can configure apache, you would just turn on mod_rewrite...

Re: 404 Redirect as mod_rewrite substitute

Posted: Sun Jun 08, 2008 11:27 pm
by Verminox
I just realised that the whole ErrorDocument redirection loses all POST data, so this method fails :(

Re: 404 Redirect as mod_rewrite substitute

Posted: Sun Jun 08, 2008 11:49 pm
by allspiritseve
Verminox wrote:But we don't always have hosts who have mod_rewrite enabled, and some might even deny our humble request. What do we do then?
Maybe it is time to start looking for a new host? http://www.webhostingtalk.com/ is a site I've used in the past.