SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    UK
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Which is best of these 3 options for converting freindly urls?

    I think I have three options for dealing with my 'freindly' urls, has anyone got a view on which is best? (efficiency, security, other?).

    Option1:
    Use mod_rewrite to substitute as follows:

    example.com/categoryX/typeY/thingZ
    --> example.com/index.php

    then in index.php explode the REQUEST_URI to get at the values:

    $urlarray=explode("/",$_SERVER['REQUEST_URI']);
    $category=$urlarray[1];
    etc.

    Option 2:
    Use mod_rewrite to substitute as follows:

    example.com/categoryX/typeY/thingZ
    --> example.com/index.php?category=categoryX&type= etc.

    then use $_GET['category'] in index.php

    Option3:
    Use mod_rewrite to substitute as follows:

    example.com/categoryX/typeY/thingZ
    --> example.com/index.php/categoryX/typeY/thingZ

    then use $urlarray=explode("/",$PATH_INFO);

    Thanks, Jack.

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Definitely the second one, with 'get -- ' just because this way your script doesn’t fail without mod_rewrite.

    Generally, always try to code in "mod_rewrite"-independent way. "Core" scripts should not rely on presence of mod_rewrite.

  3. #3
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    UK
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Stereo, but I think I am still dependent on mod_rewrite for Option 2 as the incoming URL does not contain the script name index.php - I would rely on mod_rewrite to insert it - does this make any difference to your advice?

    <edit> ... and none of the links would work without mod_rewrite as there would be nothing in the query string ...

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm... I meant as long as index.php gets the params "normally" i.e. via "GET", it doesn't have to know anything about mod_rewrite. If you turn rewrite off, your script keeps working...

    Illustration:

    Code:
    # .htaccess
    RewriteRule ^catalog/(.*)$ catalog.php?path=$1 [QSA]
    # catalog/music/cd/123 becomes
    # catalog.php?path=music/cd/123
    PHP Code:
    // catalog.php

    //this single line is mod_rewrite dependent
    // comment it out if you turn rewrite off
    ob_start('do_rewrite');

    $path $_GET['path'];
    // parse the path and do db stuff etc

    // echo html WITHOUT mod_rewrite
    echo "<a href='catalog.php?path=books'>....
    echo "
    <a href='catalog.php?path=dvd'>....
    etc

    // convert 'normal' urls to 'human readable'
    // called only if rewriting is active
    function do_rewrite($buf) {
       return 
    preg_replace("~catalog\.php?path=([a-z/]+)~""catalog/$1"$buf);


  5. #5
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    UK
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hm... hm... You are suggesting that I should include code to allow for the possibility of mod_rewrite not being available? (and I thought I was just getting to grips with this - I have never used ob_start!).

    Is this important? - I'm not sure why mod_rewrite should ever not be available for me.

    If I were to assume that mod_rewrite were always available would Option 2 still be best?

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My point is that the different parts of code should be so independent as possible, especially when it doesn’t cost you anything.

  7. #7
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    UK
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again Stereo, I take your point.

    I am still looking for opinions on the best option - assuming that I am assuming that's its ok to be dependent on mod_rewrite.

    Other thoughts?

  8. #8
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    "Core" scripts should not rely on presence of mod_rewrite.
    IMO, "core" scripts shouldn't use $_GET either, so the point is moot. The whole "untrusted data" thing - there should be some sort of filter between the core and the data comming from the user so changing from clean to messy URLs should be the filter's job, nothing to do with the core. That way you can pick which filter to load based on what sort of URLs you are using.

    Of the three options, I'd probably go with Option 2 or 3 becuse then you shouldn't have problems if you feel like putting your whole script in a subdirectory at a later stage. Instead of example.com/categoryX/typeY/thingZ you should be able to go to example.com/myApplication/categoryX/typeY/thingZ wthout much effort.

    At the same time, I would rather handle the naming of the different parts in PHP than in mod-rewrite, because if I want to say, have "typeY" get called "optionY" I can just do mapping from $optionY = $request[0] rather than the potentially confusing: $optionY = $_GET['typeY'].

    That would make my choice: Option 3.

    There are other ways. If you want to make your first level something functional (say, "article" or "download" rather than a category) then they will changeless often. Then you can use ForceType to make all files with no extension execute as PHP. This way you can distribute some of your code, so that say, a class 100% specific to dealing with articles could go in the "article" file, and then you don't have to worry loading extra Articles code when the person is visiting the Downloads section.

    hth,
    Douglas
    Hello World

  9. #9
    SitePoint Zealot
    Join Date
    Aug 2002
    Location
    UK
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Doug and Stereo.

    I get the impression there are no really 'killer' reasons as to why one option is better than another, and therefore I just need to weigh up the pro's and con's you have both mentioned in terms of my own situation (I'm not very quick at decisions - I'll be pondering it for a while).

    Cheers,
    Jack.


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
  •