Rewriting a URL

Discussion in 'PHP' started by Weirfire, Mar 6, 2006.

  1. #1
    This isn't a mod rewrite before all you exciteable MOD rewriters get turned on.

    I am doing a mod rewrite but this part is to regenerate the HTML code.

    I have the link

    $link = "www.domain.com/index.php/cPath/35_120_134";

    and I want to turn this into

    www.domain.com/category_name-35-subcategory_name-120-subsubcategory_name-134.html

    I've been using lots of strpos and substr's but I thought there might be an easier way of doing it. Anyone got any ideas? :)
     
    Weirfire, Mar 6, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    So you Mod rewrite it first and then take the raw text and let PHP take it apart?

    Considering www.domain.com/index.php/cPath/35_120_134 is already rewritten, why not amend your re-write rule?

    Sounds like a mighty complicated way of doing it.
     
    T0PS3O, Mar 6, 2006 IP
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    Weirfire, Mar 6, 2006 IP
  4. eiso

    eiso Peon

    Messages:
    583
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #4
    eiso, Mar 6, 2006 IP
  5. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #5
    Nah, I know how to mod rewrite stuff. Its more replace functions in PHP I'm looking for.

    Thanks for the resource though :)
     
    Weirfire, Mar 6, 2006 IP
  6. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #6
    OTTOMH:

    Explode the URI by "/".

    $uri_parts = explode("/", $uri);
    $part_to_rewrite = $parts[3];

    Explode last part by "_".
    $id_parts = explode("_", $part_to_rewrite);

    Search category name:

    $name1 = SELECT category_name from categories WHERE id = $id_parts[0];
    $name2 = SELECT category_name from categories WHERE id = $id_parts[1];
    $name3 = SELECT category_name from categories WHERE id = $id_parts[2];

    $new_url = $parts[0] . '/' . $name1 . '-' . $id_parts[0] . '-' . $name2 . '-' . $id_parts[1] . '-' . $name3 . '-' . $id_parts[2] . '.html';

    // Code for illustrational purposes only.
     
    T0PS3O, Mar 6, 2006 IP
  7. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #7
    rofl OTTOMH

    Thanks TOPS. That was done in about 12 lines less than I had. :)
     
    Weirfire, Mar 6, 2006 IP