[Zope3-dev] Apache rewrite rules and URLs: an experiment
Peter Mayne
PeterMayne at ap.spherion.com
Mon Oct 25 22:01:02 EDT 2004
I've set up a rewrite rule in Apache 2.0.52 to pass requests through to
Zope, as shown in chapter 4 of Stephan's book. The rewrite rule worked
correctly, but it took me a while to realise it, because of the way I'd
written my hrefs.
My application uses a skin called "imdb", so I have to make sure that
requests that reach Zope have "++skin++imdb" in the URL. As I'm
developing my application, I can manually include the skin, but I'd like
to hide this from the users, so my rewrite rule adds the skin to the URL.
Therefore, my application must use URLs that make sense whether I'm
accessing Zope directly, or via the Apache front end.
The rewrite rule is:
RewriteRule ^/imdb(/?.*)
http://chmeee:8080/++skin++imdb/++vh++http:wu:80/imdb/++$1 [P,L]
where Zope is running on chmeee, and Apache is running on wu.
In my configure.zcml for the skin, I define the resource "z3logo.gif" in
the "imdb" layer as follows:
<resource name="z3logo.gif" file="resources/zope3logo.gif" layer="imdb"/>
This means that I have to use the URL "/++skin++imdb/@@/z3logo.gif" to
access this resource: if I use "/@@/z3logo.gif", I (correctly) get a
NotFoundError.
I added the following code to the skin's index.html page:
<span tal:replace="python:request.getApplicationURL(path_only=True)"/>
<br/>
<a href="/@@/z3logo.gif">absolute</a>
<br/>
<a href="@@/z3logo.gif">relative</a>
<br/>
<a href="/++skin++imdb/@@/z3logo.gif">absolute with skin</a>
<br/>
<a href="++skin++imdb/@@/z3logo.gif">relative with skin</a>
<br/>
<a tal:attributes="href
python:request.getApplicationURL(path_only=True) +
'/@@/z3logo.gif'">absolute with path</a>
Obviously, including the hostname in the URL isn't a good idea.
First, I tried accessing the page directly, at
http://chmeee:8080/++skin++imdb/.
The application URL is "/++skin++imdb".
The resulting URLs are:
http://chmeee:8080/@@/z3logo.gif
http://chmeee:8080/++skin++imdb/@@/z3logo.gif
http://chmeee:8080/++skin++imdb/@@/z3logo.gif
http://chmeee:8080/++skin++imdb/++skin++imdb/@@/z3logo.gif
http://chmeee:8080/++skin++imdb/@@/z3logo.gif
Only three of these are correct: the relative URL, the absolute with
skin URL, and the absolute with path URL. (Although the relative with
skin URL returns the resource, having the skin in the URL twice isn't
what we want.)
Next, I tried accessing the page via Apache, at http://wu/imdb/.
The application URL is "/imdb".
The resulting URLs are:
http://wu/@@/z3logo.gif
http://wu/imdb/@@/z3logo.gif
http://wu/++skin++imdb/@@/z3logo.gif
http://wu/imdb/++skin++imdb/@@/z3logo.gif
http://wu/imdb/@@/z3logo.gif
Only two of these are correct: the relative URL, and the absolute with
path URL. (Again, the relative with skin URL returns the resource, but
Zope sees the skin in the URL twice, so we'll put this down as incorrect.)
Therefore, there are only two ways of specifying URLs that work properly
when accessed directly and via Apache. If you know where your target is
relative to your current page, use a relative URL. If you can't use a
relative URL (for instance, a link in a template to the home page), you
have to use request.getApplicationURL(path_only=True) to create an
absolute URL.
I also tried the following rewrite rule:
RewriteRule ^/imdb(/?.*)
http://chmeee:8080/++vh++http:wu:80/imdb/++/++skin++imdb/$1 [P,L]
where the skin comes after the virtual host, rather than before. This
time, none of the URLs are correct: though the relative with skin and
absolute with skin URLs work, they have the skin in the URL twice, and
although the relative URL works, it contains a double slash. (I'm being
quite picky here. :-) Furthermore, the user ends up seeing the skin in
the URL as well, so this probably isn't a good rule to use.
Comments? Stephan, am I missing something?
PJDM
--
Peter Mayne
Spherion Technology Solutions
Canberra, ACT, Australia
"You're given the form, but you have to write the sonnet yourself.
What you say is completely up to you." - Mrs. Whatsit
More information about the Zope3-dev
mailing list