#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    SEO url using php


    Hello,
    Need help with creating seo url using php, I have cattegory list on page cat.php where I generate URL
    using this code:

    <a href="item.php?id=<?=$values[$i]['itemid']?>&<?=$values[$i]['itemname']?>

    As result I get what I want but with bunch of spaces that creates URL with lots of %20
    I would like to replace %20 with dash
    I can not use mod_rewrite since there is unknown amount of spaces so I need to do it using php.
    For one item there are 4 links and I should do it separate for all 4 links so it would be good if there is any way to modify
    this line of code or put some code right before it so it points just for that link

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,505
    Rep Power
    594
    I believe preg_replace is what you are looking for.

    Comments on this post

    • grooveszk agrees
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    str_replace replaces strings, but explain why you can't use mod rewrite?

    Comments on this post

    • grooveszk agrees
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by ManiacDan
    str_replace replaces strings, but explain why you can't use mod rewrite?
    Dont know just wont work, I use this rule

    RewriteRule ^(/?id=*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
    RewriteRule ^(/?id=*/[^/]*?)_([^/_]*)$ $1-$2 [R=301]

    and thing is that my url does not change, there is still %20, but funny thing is that when I manualy change %20 to - in url and hits Enter, URL is fine with -

    With php I have also tried with str_replace or pereg_replace, weather I make it as function or not, I can not make it work just with single link
    <a href="item.php?id=<?=$values[$i]['itemid']?>&<?=$values[$i]['itemname']?>
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,129
    Rep Power
    9398
    Originally Posted by grooveszk
    I can not use mod_rewrite since there is unknown amount of spaces
    Actually you can't use mod_rewrite because that doesn't fix the URLs you output. str_replace() can replace spaces with hyphens.
    Then fix item.php so it does the reverse replacement on the item name.

    If you want friendly URLs then that's something completely different.

    Comments on this post

    • grooveszk agrees

IMN logo majestic logo threadwatch logo seochat tools logo