Changing the dynamic URLs to static URLs

46 views
Skip to first unread message

BlueShadow

unread,
Mar 14, 2013, 1:45:40 PM3/14/13
to web...@googlegroups.com
Hi,
I was wondering if it is possible to change the dynamic URLs to static ones. eg www.mydomain.com/Controler/Function?id=34 to www.mydomain.com/titleofpage
So far I removed the controler by setting it in the routes.py as default controler.
I know that you could do it manually by using the .htaccess file with mod rewrite.
In my sample domain lets say the functions name would be viewArticle and the parameter id=xx the id number of the article. lets say the title of the article is "web2py - An awesome framework"
I would love to have this URL www.mydomain.com/viewArticle_web2py_An_awesome_framework
any ideas other than mine to write to the htaccess file every time a new article is written? Well I know one could do it by python code.

Massimo Di Pierro

unread,
Mar 14, 2013, 1:54:02 PM3/14/13
to web...@googlegroups.com
routes_in = [('/titleofpage','/Controler/Function?id=34')]

Jonathan Lundell

unread,
Mar 14, 2013, 1:58:01 PM3/14/13
to web...@googlegroups.com
The parametric router will give you a URL like:


...and if you list the functions in viewArticle and make it the default controller, you can use:


Of course, viewArticle needs to know how to look up an article by its slug (which it will find in request.args(0))


FWIW, if you only (or mostly) view articles, I'd choose a name more like:


BlueShadow

unread,
Mar 14, 2013, 3:15:49 PM3/14/13
to
If I'm not completly mistaken you can't use the routes_in with the new router setup. which I use :)

BlueShadow

unread,
Mar 14, 2013, 3:20:44 PM3/14/13
to web...@googlegroups.com
Jonathan Lundell my default controler is default.py which has the funktion viewArticle by using the router I said that default.py is the default controler so this is no longer in the URL.

Jonathan Lundell

unread,
Mar 14, 2013, 3:23:05 PM3/14/13
to web...@googlegroups.com
On 14 Mar 2013, at 12:20 PM, BlueShadow <kevin....@gmail.com> wrote:
Jonathan Lundell my default controler is default.py which has the funktion viewArticle by using the router I said that default.py is the default controler so this is no longer in the URL.

Then you should be nearly there. What does your current routes.py look like?

Kevin Bethke

unread,
Mar 14, 2013, 3:32:12 PM3/14/13
to web...@googlegroups.com
#!/usr/bin/python
# -*- coding: utf-8 -*-


routers = dict(
  BASE  = dict(
      domains = {
          'mydomain.de' : 'App1',
          'www.mydomain.de' : 'App1',
          'ip' : 'App2',
          'ip/Test' : 'TestEnv',
          'ip/MA' : 'dbmigratetest',
      }
  ),
)
logging = 'debug'
def __routes_doctest():
...
I just noticed I never set the default controler. I only did that in my old routes.example based file. but it still has the effect: the controler is removed from the URL

--
 
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/OqRd6PgWbJI/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jonathan Lundell

unread,
Mar 14, 2013, 3:42:25 PM3/14/13
to web...@googlegroups.com
On 14 Mar 2013, at 12:32 PM, Kevin Bethke <kevin....@gmail.com> wrote:
#!/usr/bin/python
# -*- coding: utf-8 -*-


routers = dict(
  BASE  = dict(
      domains = {
          'mydomain.de' : 'App1',
          'www.mydomain.de' : 'App1',
          'ip' : 'App2',
          'ip/Test' : 'TestEnv',
          'ip/MA' : 'dbmigratetest',
      }
  ),
)
logging = 'debug'
def __routes_doctest():
...
I just noticed I never set the default controler. I only did that in my old routes.example based file. but it still has the effect: the controler is removed from the URL 



So, what's happening now that's different from what you want?

Kevin Bethke

unread,
Mar 14, 2013, 4:21:04 PM3/14/13
to web...@googlegroups.com
when I visit the funktion showArticle?id=1 , I want the URL to say www.mydomain.de/showArticle/TitletextofdbArticlewithid1
table article
Fields{id,title,content}

--

Jonathan Lundell

unread,
Mar 14, 2013, 4:44:33 PM3/14/13
to web...@googlegroups.com
On 14 Mar 2013, at 1:21 PM, Kevin Bethke <kevin....@gmail.com> wrote:
when I visit the funktion showArticle?id=1 , I want the URL to say www.mydomain.de/showArticle/TitletextofdbArticlewithid1
table article
Fields{id,title,content}

That's up to you when you call URL().

Suppose you're now asking for URL('showArticle', vars={'id':article_id}). You'll need to change that to URL('showArticle', args=[slugForArticleID(article_id)])

You can use IS_SLUG() for the title-to-slug conversion (note that you'll need to check that you don't have collisions) and keep the slug as well as the title in the article table, so you can convert both ways.
Reply all
Reply to author
Forward
0 new messages