-1

Possible Duplicate:
Routing URLs in PHP

How do you set up virtual URLs like typical CMS frameworks? Is there a good tutorial or code bank? From what I understand it reroutes all requests to the index.php and Apache does some sort of mod_rewrite. I'm not totally sure though. How would I start setting up a framework like this?

Community
  • 1
  • 1
Graham
  • 1,433
  • 3
  • 21
  • 34

3 Answers3

1

You are correct with the mod_rewrite, here is a couple of good tutorials on the matter, it also goes into various other uses of the .htaccess file

http://corz.org/serv/tricks/htaccess2.php

http://www.workingwith.me.uk/articles/scripting/mod_rewrite

no.
  • 2,356
  • 3
  • 27
  • 42
0

You should enable mod_rewrite in Apache.

You are right about rerouting all requests to index.php

Once your done with all of that your .htaccess file should contain something like this:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

This basically tells all request to go to index.php

I would suggest checking out existing frameworks like CodeIgniter and find out how they do it, then implement your own.

IMB
  • 15,163
  • 19
  • 82
  • 140
  • It's not necessary to send all requests to `index.php`. – Michael Mior Oct 13 '11 at 17:48
  • Well of course by "all", I mean excluding images and other stuff like my htaccess example code. – IMB Oct 13 '11 at 17:56
  • Sure. But what you're proposing is the [front controller pattern](http://en.wikipedia.org/wiki/Front_controller) which is an extreme case of URL redirection. In many cases, it makes more sense to have several rewrite rules for different parts of the application. – Michael Mior Oct 13 '11 at 18:56
  • Yup that's exactly what I'm proposing. Isn't that what's being done in most MVCs, why is that bad? – IMB Oct 13 '11 at 19:24
  • Not saying it's necessarily bad. I would just suggest the answer isn't complete without explaining this. – Michael Mior Oct 13 '11 at 20:08
  • 1
    +1 as I am not sure why it was downvoted. Perfectly reasonable answer, imo. – halfer Oct 14 '11 at 14:47
0

See how symfony does it here (as far as the mod_rewrite goes).

And check this answer for some ways to do the PHP part.

Community
  • 1
  • 1
halfer
  • 19,824
  • 17
  • 99
  • 186