Reaching pages by a descriptive pagename

29 views
Skip to first unread message

BlueShadow

unread,
Feb 10, 2014, 10:44:48 AM2/10/14
to web...@googlegroups.com
Hi I would love if I could reach my Articles not only by this type of URL: www.mydomain.com/Article?id=112 but also by this type of URL: www.mydomain.com/Article:Title-of-the-Article (or something similar I just want to have the Title of that Article in the URL). Is there an easy way to do this? The old URLs still have to work! I would love to have a redirect to my new type of URL.
Thanks for your Help

LightDot

unread,
Feb 10, 2014, 11:02:43 AM2/10/14
to web...@googlegroups.com
What you're looking for is called a "slug". Web2py makes it really easy, since it already has a validator that can create a slug:
http://web2py.com/books/default/chapter/29/07/forms-and-validators?search=is_slug

Last time I implemented this was for a concert site, creating a slug from the concert name, location, etc. Then I used the precreated slug together with the concert id in the URL args, displaying something like domain.com/concert/917/artist-city-date-venue-something-something-etc. to visitors. Basically, the id is needed, the rest is just there for the show (and SEO).

Regards

Anthony

unread,
Feb 10, 2014, 11:04:09 AM2/10/14
to web...@googlegroups.com
How about something like /Article/Title-of-the-Article? In that case, just look in request.args(0) for the article slug and use that to query the db. If it's possible for multiple articles to have the same title (or at least the same slug), then you could include the ID as an arg as well: /Article/112/Title-of-the-Article. Then just grab the ID from request.args(0). You could still include logic to check for request.vars.id for the legacy URLs.

Anthony

Kevin Bethke

unread,
Feb 10, 2014, 12:04:22 PM2/10/14
to web...@googlegroups.com
So my controller function would look something like this?
def Article():
idstring=request.args(0)
/*seperate id out of /112/-Title-of-Article*/
row=db(db.Article.id==id).select()
return dict(Article=row[0])

Anthony

unread,
Feb 10, 2014, 12:30:31 PM2/10/14
to web...@googlegroups.com
Yes, with some additional logic to handle the legacy URLs, make sure request.args(0) is an integer, etc.
Reply all
Reply to author
Forward
0 new messages