SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    S. Florida, USA
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dynamic mod_rewrite rules???

    First time trying out mod_rewrite to see if I can use this feature with php.
    I have it working, but Im not sure if what Im doing is the *best* way.

    Examples ->
    http://localhost/index.php/command/news_article
    http://localhost/index.php/command/news_article/foo/bar
    http://localhost/index.php/command/n...foo/bar/me/you

    My goal is to setup a rule that doesn't know the number of key/values, it has to rewrite.
    The only way I got this to work is to use multiple rules ->
    RewriteRule ^index.php/([a-z]+)/([a-z0-9_]+)$ index.php?$1=$2
    RewriteRule ^index.php/([a-z]+)/([a-z0-9_]+)/([a-z]+)/([a-z0-9_]+)$ index.php?$1=$2&$3=$4
    RewriteRule ^index.php/([a-z]+)/([a-z0-9_]+)/([a-z]+)/([a-z0-9_]+)/([a-z]+)/([a-z0-9_]+)$ index.php?$1=$2&$3=$4&$5=$6

    On a side note. Would it be better, if I just exploded $_SERVER['REQUEST_URI'], and populated $_GET with the data???

    Thanks for your time

  2. #2
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On a side note. Would it be better, if I just exploded $_SERVER['REQUEST_URI'], and populated $_GET with the data???
    That's what I do for my dynamic sites where I want nice URLs. I send every incoming request to /index.php with mod_rewrite. And I don't populate $_GET with it but I make up some other array, $url_vars or something.

    One caveat with this method is that magic_quotes_gpc doesn't work on it and you have to be a little extra careful with your data validation if you're used to working with that.

  3. #3
    SitePoint Addict mgkimsal's Avatar
    Join Date
    Sep 1999
    Posts
    209
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It would probably be more portable if you did the second option. It's been awhile since I ran PHP on IIS (going to again next week!) so there might be problems there, but it's certainly no *less* portable than Apache-specific mod_rewrite rules. Additionally, portability also extends to others wanting to use/test your code, either for free or for profit. The easier it is to set it up - minimal server modifications - the better.
    Michael Kimsal
    =============================
    groovymag.com - for groovy/grails developers
    jsmag.com - for javascript developers

  4. #4
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It would probably be more portable if you did the second option
    What an awesome point. I hadn't thought that using mod_rewrite is actually more portable, but of course all you'd have to do is use the IIS specific method of doing the rewrite (God knows what that might be) and the code wouldn't have to be touched. Good one.

  5. #5
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    S. Florida, USA
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    S. Florida, USA
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HardCoded
    And I don't populate $_GET with it but I make up some other array, $url_vars or something.
    I was reading this again, and noticed this.
    Could you explain why you wouldn't populate $_GET.
    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •