Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

[Apache2] Clean urls [PHP]

Name: Anonymous 2010-02-23 18:05

I am sorry if this is the wrong board. I would like to create a site with clean urls using mod_rewrite, .htaccess and php. I have searched google and tested but I just can't figure it out 100%.

I'm aiming for a system where all the arguments given in an url would be automatically transferred to GET variables with the first two being pre-named and the others would be identifiable from the previous (always in pairs of 2).

Example of URL to convert:
  http://localhost/controllerX/viewY/id/4/page/2/
Conversion result:
  $controller == controllerX
  $view == viewY
  $id == 4
  $page == 2

I have the controller and view variable part kind of sorted (I think), but I can't get the rest of the url sorted. My current .htaccess rules:
  RewriteRule ^([^/\.]+)/?$ /index.php?controller=$1 [L]
  RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?controller=$1&view=$2 [L]

In PHP I access them like: $_GET['controller'] and $_GET['view'].

Name: Anonymous 2010-02-23 18:07

Read SICP.

Name: Anonymous 2010-02-23 18:09

[code] tags.

Name: Anonymous 2010-02-23 18:47

>>2
That's merely an introductory textbook, this is a much more advanced problem.

Name: Anonymous 2010-02-23 19:16

>>4
Yeah, but he wouldn't have to ask if he'd read his SICP today...

Name: Anonymous 2010-02-24 0:03

you probably don't want to hardcode a controller and view to the url/htaccess like that. i mean, yeah, that's how things are done by default in most mvc frameworks, but you usually can do more interesting routes through the router. i'd recommend looking at how cakephp's or codeigniter's router works. cakephp is kind of a piece of shit, but there's some decent parts of it, and you just endup needing an htaccess file with one rule.

Name: Anonymous 2010-02-24 0:10

>>8036109
When I download porn (naked pictures, not videos) I feel like I am kidnapping a little piece of whoever's in them, and hiding it in my basement. I don't actually look at porn once I've got it. I just find lots and lots of pictures that I want to download, because it's like if I could download enough of them, I could get an entire woman in my basement. I don't think I would know what to do with her if I did really get her there. If I ever figure that out, I might really want to do it then.

Name: OP 2010-02-24 5:11

>>3
Sorry about that, will do the next time I post code.

>>6
Thanks for the input - I'll have a look at routing next. When trying out my current rules I did have some nightmares about (overloading?) every possible GET-combination in my .htaccess. Any system with a better basic structure sounds great.

Name: Anonymous 2010-02-26 13:36

>>8
Rewrite everything except /index.php to go to /index.php?$1 and split the pairs in PHP instead.

Don't change these.
Name: Email:
Entire Thread Thread List