My name is Mladen Milankovic, and I'm a first time poster. I've been working
with web2py for a couple of months (reading mailing list from that time), and
around a year with python.
I like writing applications in web2py, because it's easy and fast... I came
from php. :)
Wanted to use web2py through apache. At first I used it through mod_proxy, but
wanted to go to wsgi. I manage several domain names and needed to access
different application from web2py as if they run through the root, without the
application/controller/function. I managed to make it work like needed through
mod_proxy, but rewrite through wsgi was hard for me. routes.py was much
easier, at least for me, but I could only setup one set of rewrite
instructions in it.
I saw AchipAs post in "web2py and GAE" thread about modifying the routes.py so
it can be given parameter about host, so different host names have different
rewrite instructions. Unfortunately this was the last post I saw about this
topic.
Thought to give it a try and write a patch that will do that. I tested it
directly and through proxy and wsgi. It's backward compatible. Didn't have any
problems with it. Further testing needed.
I went with AchipAs solution of third parameter to every rewrite instruction:
example
routes_in = (('.*:/favicon.ico', '/examples/static/favicon.ico'),
('.*:/robots.txt', '/examples/static/robots.txt'),
('.*:/robots.txt', '/myapp/static/robots.txt', 'mydomain.com'),
('.*:/', '/myapp/default/index', 'mydomain.com')
)
Now when /robots.txt is called through mydomain.com it will give the one from
/myapp/static/, and when called through any other the one from
/examples/static/. Also mydomain.com/ will give /myapp/default/index page.
Same rules apply for routes_out and routes_onerror. Host name can be written
in regular expression, and instructions with host name will be prioritized.
They will be checked first.
Modified files:
/gloun/rewrite.py
/gluon/main.py
Unfortunately, had to modify gluon/main.py, too. It contains check_error_route
function, as I read in the comment because of IE. Tried it only with Firefox.
Found on net how to create svn patch. It created against svn, revision 796.
regards
mmlado
Oops...
This was implemented in 1.61 but with a slightly different syntax than
you proposed:
routes_in=(('^127.0.0.1:https://localhost:post /hello','/admin'))
i.e. re-route all https POST requests from 127.0.0.1 to
http_host=="localhost" to /admin.
Thank you very much. This is more powerful and more useful then what I did. I
see I have a lot to learn.
regards
mmlado
A request comes in and you want to redirect it to a controller action
filtering by
- IP of client (for example 127.0.0.1)
- requested protocol (for example http or http "(http|https)""
- requested hostname (for example www.web2py.com)
- type of request (for example get or post "(get|post)")
- requested URL (for example "/test")
You would capture it with
"^[client]:[protocol]://[hostname]:[method] [url]$"
in the example
"^127.0.0.1:(http|https)://www.web2py.com:(get|post) /test"
Now you can map this into for example "/welcome/default/index" with
routes.py
routes_in=(("^127.0.0.1:(http|https)://www.web2py.com:(get|post) /
test", "/welcome/default/index"),)
I can see writing an entire book in this only.
Can you figure out what this does?
routes_in=(("^127.0.0.1:$a://www.web2py.com:(get|post) /test/$b", "/
welcome/default/$b?method=$a"),)
and this?
routes_in=(("^127.0.0.1:$a://www.web2py.com:(get|post) /test/(?
P<b>.*)", "/welcome/default/$b?method=$a"),)
I know it is ugly but 1) it works; 2) it very powerful when compared
with routes on rails and urls in Django; 3) is is backward compatible.
I'm testing things out and I think got a bug.
I'm trying to get root on localhost to show /my_app/default/index page and not
the example, so I wrote:
routes_in = (('^.*?:https?://localhost:[a-z]+ /$', '/my_app/default/index'), )
It didn't work, and I throw in some logging. I watched what will be the key
before it's converted to regex. It gave:
^.*?:https?:https?://[^:/]+:[a-z]+ //localhost:[a-z]+ /$
Problem is in
line 45, in rewrite.py
if k.find(':/') > 0:
This line is determining if the definition is in the old format:
'.*:/favicon.ico'.
Problem is that in the new definition :/ can be found again, but it has a
different meaning.
It can be done in many different ways. I used this one:
if k.find(':/') > 0 and not k.find('://'):
This way it will match only the old definition and skip the new one.
regards
mmlado
On Thursday 16 April 2009 19:57:47 mdipierro wrote:
Why you do not recommend using routes.py, like in:
http://www.web2py.com/AlterEgo/default/show/42
?
I think this is very useful, including for create nice and more
readable URLs (I don't like a "/default/" or some long URLs created by
default).
Does routes.py decreases web2py performance in an way that its use is
discouraged?
--
Álvaro Justen
Peta5 - Telecomunicações e Software Livre
21 3021-6001 / 9898-0141
http://www.peta5.com.br/
How I can change wiki?
I want to change the begin of http://www.web2py.com/AlterEgo/default/show/42
to:
"web2py supports URL rerwite - it's a very useful feature.
You can use Apache (or lighttpd) + mod_proxy (or mod_rewrite) for this
purpose but using web2py's rewrite you have more flexiblity.
***routes.py syntax changed so much in early releases (like 1.61).
This page have to be updated.***
To use this feature just create a new file in web2py/ called routes.py
and write something like this in it..."
So, if it is a nice feature, why it generates a warning if I use routes.py? :-P
WARNING:root:URL rewrite is on. configuration in routes.py