Making keyword rich, human readable URLs

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

It's all about how you deal with duplicates. :-/
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I never have duplicates. I include the ID i've pulled from a database in the URL.

forums/view-topic/12345/some-topic-title.html
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

untitled or a similar variant (make it configurable) sounds reasonable to me.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Good call. I edited the original post to include another parameter that defaults to 'no-title'. Lots of paramter calls =/ But the function works!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

At this point I would have turned into a class...

Note that using empty() will also cause pages called "0" to be switched to blank.
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

What about just failing on a "null" input... if i enter !@#!@#*!&@#(*&!@# it's going to spit out '', but instead of blindly accepting it and turning it into something the user wont be expecting (and therefore probably just re-enter the same data), you just return to the form page with an error, "Only use a-z,0-9,etc in your title"
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Ambush Commander wrote:At this point I would have turned into a class...

Note that using empty() will also cause pages called "0" to be switched to blank.
I probably will classalize it. Good call with the empty(), I would've never caught that myself.
nickvd wrote:What about just failing on a "null" input... if i enter !@#!@#*!&@#(*&!@# it's going to spit out '', but instead of blindly accepting it and turning it into something the user wont be expecting (and therefore probably just re-enter the same data), you just return to the form page with an error, "Only use a-z,0-9,etc in your title"
That would defeat the purpose of what this does. If they put in something like that, they likely don't care about the title anyways. :P Besides, their original title will still show up on the page, this is just for a URL.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Alright, I turned it into the shortest class ever. I added a default maxlength of 50. I probably won't do too much more to it. Hopefully someone else will find it useful. ;d Thanks for the tips everyone!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

scottayy wrote:Hopefully someone else will find it useful.
Have you posted it in the code snippets?
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post by DaveTheAve »

*cough* Don't Look Here *cough*
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

DaveTheAve wrote:*cough* Don't Look Here *cough*
I already suggested that... it's not what he's looking for... this is for something else.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

A lot of you are missing the point :P

The point is keyword rich - human readable, urls.

For example, I recently started using this on my forums.

The world news & current events forum

- Before - showforum.php?forumid=21
- After - forums/21/world-news-and-current-events/index.php

A topic in that forum.....

- Before - showthread.php?threadid=22107
- After - forums/view-topic/22107/yet-another-school-shooting.php

That would look terrible if it were yet%20another%20school%20shooting%2E.php
Have you posted it in the code snippets?
I can't, it's a closed forum.
Last edited by s.dot on Wed Oct 04, 2006 3:31 pm, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

yea I understand what you're trying to do now scottayy
User avatar
DaveTheAve
Forum Contributor
Posts: 385
Joined: Tue Oct 03, 2006 2:25 pm
Location: 127.0.0.1
Contact:

Post by DaveTheAve »

scottayy wrote: - Before - showthread.php?threadid=22107
- After - forums/view-topic/22107/yet-another-school-shooting.php

That would look terrible if it were yet%20another%20school%20shooting%2E.php
Well, that was done with .htaccess.... and you could implatment the dashes with this:

Code: Select all

$url = urlencode($_GET['url']);
$url = str_replace("%20","-",$url);
note that was not tested and will most likely not work but it was just a idea to throw out there.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Certainly a useful function/class. Some CMS systems do the same thing (like wordpress). It automaticly turns the title you choose for an article into the permalink and nice url, in a way you showed (nice-title-of-the-article).

Good work scottayy (and others), I might use the function some day.
Post Reply