Saturday, July 29, 2006

Embedding and extracting primary key in a clean URL

Clean URLs are very important for search engines and for that reason I always prefer them.

A url of the form is better than

An even better form of the URL is where article-title represents the important keywords that appear in the title.

When using Ruby on Rails, you can create the permalink from title using the following function:

def create_permalink(string)
string.gsub(/([^ a-zA-Z0-9_.-]+)/n) do
'%' + $1.unpack('H2' * $1.size).join('%').upcase' ', '-').downcase


and you can extract the id from this permalink using a function given below:

def get_id_from_permalink(permalink)
@temp_array = permalink.split('-')
return id

eXTReMe Tracker