Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    Jan 2009
    Posts
    193
    Thanks
    0
    Thanked 20 Times in 20 Posts

    friendly urls and dealing with hyphens

    I am using mod rewrite to create friendly urls and I am using hyphens to deal with spaces. My issue is how do I deal with items that already contain hyphens?

    For example

    If I have "foo bar"

    then I the url would be
    example.com/foo-bar

    If someone visits the page then I use php to replace hyphens with spaces and look for name = "foo bar" in the database.

    My problem is what if the wording already has a hypen like "foo-bar" then the url will also be example.com/foo-bar. Then my string replace of hyphens to spaces doesn't work because the query to the database will be looking for name = "foo bar" instead of name = "foo-bar".

    How do I solve this issue?

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,974
    Thanks
    15
    Thanked 229 Times in 229 Posts
    Why not replace the space with a string that is highly unlikely to be found in a URL such as !|! , you're not restricted to replacing one character with a single other character.

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Put the entry id into the URI rather than relying on a subject search. Either that or you'll need to use % between your words in the query, but that ain't going to be the best choice.

  • #4
    Regular Coder
    Join Date
    Jan 2009
    Posts
    193
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Quote Originally Posted by MattF View Post
    Put the entry id into the URI rather than relying on a subject search. Either that or you'll need to use % between your words in the query, but that ain't going to be the best choice.
    I have been considering that, but I am also just curious on how other sites handle the issue.

  • #5
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Why not do what most common sites do and use both an id number and the text for example the url can be example.com/forum/topic-1-1092.html the "1092" is the id and the "topic-1" in the name but the real name is "topic 1" and for the sql query you just write.

    SELECT * FROM `topics` WHERE `id` = '1092' && `title` LIKE 'tpoic-1' LIMIT 0, 1

    This way you get the correct item but will also be checking the title as well.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #6
    Regular Coder
    Join Date
    Jan 2009
    Posts
    193
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Quote Originally Posted by DJCMBear View Post
    Why not do what most common sites do and use both an id number and the text for example the url can be example.com/forum/topic-1-1092.html the "1092" is the id and the "topic-1" in the name but the real name is "topic 1" and for the sql query you just write.

    SELECT * FROM `topics` WHERE `id` = '1092' && `title` LIKE 'tpoic-1' LIMIT 0, 1

    This way you get the correct item but will also be checking the title as well.
    I will probably go that route, but I am also curious on how other developers solve this issue.

  • #7
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Web developers normally use the LIKE handler when dealing with strings that have more than one way to detect it as the MYSQL query deals with the detection of multiple outcomes.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #8
    Regular Coder
    Join Date
    Jan 2009
    Posts
    193
    Thanks
    0
    Thanked 20 Times in 20 Posts
    I think the route I need to go is just storing the url value.

  • #9
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Why not call out all url's in your database strip out all the hyphens and do the same with the url of the browser and then check to see if that url is in the database as this way all hyphens have been stripped on both url's and then it be stored in the database with the hyphens added in as this way the hyphens would never be checked whether the url includes the hyphens or not.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #10
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Quote Originally Posted by skywalker2208 View Post
    I will probably go that route, but I am also curious on how other developers solve this issue.
    I've used two solutions. My CMS is purely string based and replaces spaces with underscores. The clients are aware they can't use underscores in a page title.

    My ecommerce framework uses an identifier. The product 57 called "Something" would be /p57/Something.html. The category 3 called "My Category" is /c3/My_Category.html.

    The beauty of the latter solution is with shortened URLs. The framework only does lookups based on the p57 or c3, checks the URL name matches the database and redirects to the correct URL if necessary. This means you can have /p57/Wrong.html and have it auto-correct to /p57/Something.html. More importantly, you can post to twitter or similar as /p57 and avoid using URL shortening tools. This is also resilient to page name changes, and notifies Google when it next crawls your site.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog


  •  

    Posting Permissions

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