404 like mod_rewrite?

  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Well, Just wondering if it would be possible to emulate the effects of mod_rewrite, using a 404 page? You could obtain the URL requested using:
PHP Code: [ Select ]
 
$_SERVER['REQUEST_URI']
 
 
  1.  
  2. $_SERVER['REQUEST_URI']
  3.  
  4.  
(assuming PHP) and then determine what to do based upon this and include() relevant files/scripts.

The idea is to use on a db driven site, so if a static file exists, that is sent, otherwise the 404 kicks in and potentially serves up a dynamic page (unless no record is found, in which case it genuinely 404's)

I'm sure I thought this idea through a couple of weeks ago and came to the conclusion it was a bad idea or that it would not work or something, but I can't for the life of me remember why :? If someone can point out to me why it wouldn't work I would appreciate it to stop me getting halfway through and having to backtrack. If someone can confirm it <b>would</b> work, I would apreciate it even more, as I have now started drawing my little flowcharts and properly planning this system :D
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Hmmmmm, are 404 errors logged by apache, and if so, would this kind of system make super-bloated log files? Can you turn off logging of stuff like that?

Anyone got any idea on what the effect of this would be, or would it not really matter?
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

I'm fairly certain that what you are asking for can be done using mod_rewrite. While working on my forum software I needed some complex mod_rewrite rules. And I found a very good mod_rewrite resource:

http://modrewrite.com/

//edit: I am fairly certain that, in most cases, 404's are logged just like any other page request but with 404 instead of 200. I'm not sure, though. Wether they are or not, it is most likely quite easy to change that particular configuration option in apache.

//edit2: I am sure it is possible to do what you are asking using mod_rewrite, but I can't figure out how Why not just rewrite everything to your PHP script and then check to see if a file exists using your script. If it does, read it in, echo it out, and exit(). If it doesn't, do your thing with the database. If [it]that[/i] doesn't work, use header() to return a 404 error. Simple.
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

Thanks for the input and the link, I'll have a look in a bit. I know that mod-rewrite COULD do it, but it just seems to flow nicer with the 404 idea. Plus it has the advantage that any static files will not need processing by php, in the way that you suggested, but that is only a minor performance tweak.

In terms of actually using mod-rewrite, I was thinking along the lines of using

http://www.domain.tld/path/to/file/ for dynamics and
http://www.domain.tld/path/to/file.ext for statics

for the naming system either way. Which works fine as long as you don't use index.html for your statics (no big deal to me)

Of course all transactions are logged so it probably wouldn't make any difference, although I guess it would mess up the analog-type program readings for errors, but never mind.

I guess it would be possible to do either way, I mocked up some processes and the only thing that creates confusion between the two is how I planned to handle errors (obviously if I was using a 404 to initiate the script I could not use a 404 header from that!), but that is workable.
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

Here's a thought. I very much doubt that apache will parse a 404 page as PHP. That's probably the reason you ditched the idea two weeks ago :lol:

Assuming apache does parse the script,...

I don't really understand your last paragraph. Why could you not use header() from within the PHP script to change the response conde from 404 to 200 and vice versa?

Another thought. If you output a 404 response code and then output your content, some really crappy browsers (*cough* IE *cough*) will replace your content with their own 404 page (ie "This page cannot be displayed"). So make sure you are outputting the right response code. Use header()
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

Btw, I still think that working out the relevant mod_rewrite rule is the best method.
  • rtm223
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1855
  • Loc: Uk

Post 3+ Months Ago

rjstephens wrote:
Here's a thought. I very much doubt that apache will parse a 404 page as PHP.

It does. My 404 page include()s the common headers and footers for the page so i needed it to be php. In fact as a bonus you can also the following .htaccess declaration:
Code: [ Select ]
ErrorDocument 404 /errordocs/?error=404
ErrorDocument 403 /errordocs/?error=403
  1. ErrorDocument 404 /errordocs/?error=404
  2. ErrorDocument 403 /errordocs/?error=403
So not only can you get a custom error page to parse php (it's index.php of course), but you can also use $_GET['error'] inside that error page, which is pretty sweet and saves having multiple error docs :)

Quote:
I don't really understand your last paragraph. Why could you not use header() from within the PHP script to change the response conde from 404 to 200 and vice versa?

Well with the 404 system, the 404 error is generated on <b>every</b> page request. If you then send another 404 from the 404 handler page, you will either end up in a infinate loop (d'oh) or you will JUST get the 404 header sent (ie no custom 404 page, which generally sucks)

Quote:
Another thought. If you output a 404 response code and then output your content, some really crappy browsers (*cough* IE *cough*) will replace your content with their own 404 page

Ahh, but they won't. They will see it as a custom-404 esque page. I have a distinct feeling that if you have a custom 404 page then the 404 header does <b>not</b> get sent, I think a 200 OK gets sent, because as far as the browser need to know it <b>has</b> got a page to show, and that is what matters.

However, assuming I stick to the simple naming rule i mentioned above, the rewrite should be a doddle. Also you are probably right, rewrite is the best way to go, it all comes down to the right tool for the job.....
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

Quote:
Well with the 404 system, the 404 error is generated on every page request. If you then send another 404 from the 404 handler page, you will either end up in a infinate loop (d'oh) or you will JUST get the 404 header sent (ie no custom 404 page, which generally sucks)


If I'm not mistaken, the response code set with header() is the only one sent.


Quote:
Ahh, but they won't. They will see it as a custom-404 esque page. I have a distinct feeling that if you have a custom 404 page then the 404 header does not get sent, I think a 200 OK gets sent, because as far as the browser need to know it has got a page to show, and that is what matters.


I was on a forum once, and someone told everyone to go to a URL, and everyone else posted back that they got a "You are not authorised to view this page" error. When I went there I got a "This account has been suspended". The reason? I was using Firefox, and they were all using IE. The page was outputting a 401 or a 403, I can't remember which. So I am fairly certain that IE replaces all 404 pages with a generic page. You would have to make sure that your 404 outputted a 200.
  • Veter
  • Novice
  • Novice
  • User avatar
  • Posts: 20

Post 3+ Months Ago

IE replaces all 404 pages with a generic page only if the file (that you gave instead of 404 error) will be smaller than 1kb.

It doesnt depend from the browser. Apache does redirects it for any browser, including Lynx and even NetCat requests.

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 58 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.