404 Redirect as mod_rewrite substitute

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

404 Redirect as mod_rewrite substitute

Post 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 :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: 404 Redirect as mod_rewrite substitute

Post 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 ...
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Re: 404 Redirect as mod_rewrite substitute

Post by Verminox »

The point was to use redirects when mod_rewrite is not enabled.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: 404 Redirect as mod_rewrite substitute

Post 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)
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Re: 404 Redirect as mod_rewrite substitute

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: 404 Redirect as mod_rewrite substitute

Post by pickle »

Isn't the 404 header sent by Apache already? Would a 200 header following that be ignored, or take precedence?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Re: 404 Redirect as mod_rewrite substitute

Post 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.
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Re: 404 Redirect as mod_rewrite substitute

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: 404 Redirect as mod_rewrite substitute

Post by onion2k »

Won't that do all sorts of horrible things to your log files?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: 404 Redirect as mod_rewrite substitute

Post by Eran »

Yeah, sounds like the error log file will grow to enormous proportions if every request issues a 404.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: 404 Redirect as mod_rewrite substitute

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: 404 Redirect as mod_rewrite substitute

Post 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...
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Re: 404 Redirect as mod_rewrite substitute

Post by Verminox »

I just realised that the whole ErrorDocument redirection loses all POST data, so this method fails :(
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: 404 Redirect as mod_rewrite substitute

Post 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.
Post Reply