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 http://rubyonrails.blogspot.com/article/55 is better than http://rubyonrails.blogspot.com/?aid=55.

An even better form of the URL is http://rubyonrails.blogspot.com/article-title-55 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
end.tr(' ', '-').downcase

end


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

def get_id_from_permalink(permalink)
@temp_array = permalink.split('-')
id=@temp_array[-1]
return id
end

0 Comments:

Post a Comment

<< Home

eXTReMe Tracker