WEB how do you handle dynamic URLs in php/apache/whatever?

Discussion in 'OT Technology' started by White Stormy, Jul 29, 2008.

  1. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,489
    Likes Received:
    70
    Location:
    Sparkopolis
    say a user goes to bucket.com/of/crap

    without building the directory.. how do I handle it?

    do I just do a .htaccess redirect or something to a php file that breaks down the URL and builds the page? or is there something more elegant/better?

    this is one of the last 2 things that I think I need to get a good grip on.

    thanks
     
  2. Ender0910

    Ender0910 woot!

    Joined:
    Jun 1, 2004
    Messages:
    3,039
    Likes Received:
    0
    Location:
    Redmond/Bay Area
    yeah. in my experience, you use mod_rewrite mechanisms in the htaccess which break the folder structure down into stuff that you can pass to a php file, like you said.
     
  3. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,489
    Likes Received:
    70
    Location:
    Sparkopolis
    if I have a cache system enabled, it might have built a bucket.com/of/crap/index.php or index.html

    so can mod_rewrite check if that page exists and if not, go somewhere else? or would mod_rewrite just go to the somewhere else and then that page can look for bucket.com/of/crap/index.php and forward there if it exists?

    thanks
     
  4. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,489
    Likes Received:
    70
    Location:
    Sparkopolis
    or I guess I could just have the user go to the link bucket.com/of/crap/ and if the index file doesn't exist.. maybe I could write a 404 page that will forward to the php page that builds the crap page.. hrmm
     
  5. projectalpha

    projectalpha 二號 Stunna

    Joined:
    Aug 4, 2001
    Messages:
    17,671
    Likes Received:
    0
    Location:
    Bay Area, California
    I usually use the 404 method.
     
  6. RG

    RG New Member

    Joined:
    May 4, 2003
    Messages:
    28,233
    Likes Received:
    0
    Location:
    Connecticut
    look at existing php frameworks and read their routing class
     
  7. hank85

    hank85 sudo shred /dev/sda -f -v -z --iterations=6

    Joined:
    Jul 23, 2008
    Messages:
    4,360
    Likes Received:
    0

    You're headed in the right direction. What you're trying to accomplish is passing off pseudo-variables to either your index.php page or "of" from /of/crap.


    For example:
    http://bucket.com/index.php?query1=of&query2=crap

    OR

    http://bucket.com/of.php?query1=crap

    This is obviously the most limited of examples but it gives you an idea of what's actually going on. The way you pass your variables to your php files are all predetermined in you .htaccess file.

    I'm new to the forum but here is a link that you'll find extremely useful:
    http://corz.org/serv/tricks/htaccess2.php


    Good luck, update the post with any more questions.
     
  8. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,489
    Likes Received:
    70
    Location:
    Sparkopolis
    :bowdown:

    a redirect only changes the URL in the background, though, right?
    so if I go to bucket.com/of/crap and have .htaccess forward to a .php page.. the bucket.com/of/crap will still be in the address bar, and I could just grab that in PHP and break it down instead of converting the URL to a query string first, right?
     
  9. Ender0910

    Ender0910 woot!

    Joined:
    Jun 1, 2004
    Messages:
    3,039
    Likes Received:
    0
    Location:
    Redmond/Bay Area
    correct.

    You can either pass the entire path of/crap as a query string, or you can use the mod_rewrite to break it appart for you which may save you some php complexity.

    Here's an example for if you did a wallpaper gallery site. Originally:

    thesite.com/gallery.php?imgid=12345

    can changed to

    thesite.com/wallpapers/12345/wallpaper-title

    now, the .htaccess will have to ignore the wallpapers and the title, grab the number in the middle and pass it into gallery.php. This would be done in a single line in the .htaccess file.
     
  10. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,489
    Likes Received:
    70
    Location:
    Sparkopolis
    ahh ok, well the code to break apart the clean URL (without a query string) is really simple, but it looks like it would probably save CPU to use .htaccess to query strings instead

    :bigok: thanks
     

Share This Page