...

View Full Version : friendly urls and dealing with hyphens



skywalker2208
11-30-2010, 04:35 AM
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?

DrDOS
11-30-2010, 05:14 AM
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.

MattF
11-30-2010, 08:23 PM
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.

skywalker2208
11-30-2010, 09:53 PM
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.

DJCMBear
11-30-2010, 10:10 PM
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.

skywalker2208
11-30-2010, 10:49 PM
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.

DJCMBear
11-30-2010, 11:02 PM
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.

skywalker2208
12-01-2010, 12:01 AM
I think the route I need to go is just storing the url value.

DJCMBear
12-01-2010, 09:17 AM
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.

Lamped
12-01-2010, 09:53 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum