*This page describes how to use Apache's mod_rewrite module with
WebKit_, and mod_webkit in particular. It starts with a general
overview and then presents some recipes for solving specific
problems.*
Overview
--------
Related reading: `mod_rewrite reference docs`_, `mod_rewrite guide`_
.. _mod_rewrite reference docs: http://httpd.apache.org/docs/mod/mod_rewrite.html
.. _mod_rewrite guide: http://httpd.apache.org/docs/misc/rewriteguide.html
*mod_rewrite* is an Apache module that is used to remap URLs. Here's
how the Apache manual describes it:
This module uses a rule-based rewriting engine (based on a
regular-expression parser) to rewrite requested URLs on the
fly. It supports an unlimited number of rules and an unlimited
number of attached rule conditions for each rule to provide a
really flexible and powerful URL manipulation mechanism. The URL
manipulations can depend on various tests, for instance server
variables, environment variables, HTTP headers, time stamps and
even external database lookups in various formats can be used to
achieve a really granular URL matching.
It is an incredibly flexible tool that can be used for a wide range of
tasks. Here's some examples:
* change the file layout of your site without breaking bookmarked or
search-engine indexed URLs
* make a certain directory or file visible in every subdirectory of
your site without needing to symlink to it in every subdir or manage
complex relative paths.
* use mod_webkit to serve files that appear to the user as if they are
in Apache's top directory ('DOCUMENT_ROOT'). Without mod_rewrite,
each URL would look like _www.mysite.com/WK/MyServlet_.py_. With
mod_rewrite, you can use a simpler URL like
_www.mysite.com/MyServlet_.py_
* redirect to files on remote servers
* TestSubject_
To use mod_rewrite you need to compile it with Apache and then enable
it in your _httpd.conf_ configuration file. See the apache docs for
compilation instructions. To enable it in your _httpd.conf_ file,
uncomment or add the following lines to the DSO section in part 1::
LoadModule_ rewrite_module modules/mod_rewrite.so
AddModule_ mod_rewrite.c
-- TavisRudd_ - 05 Mar 2002
Recipes
-------
Hiding the /WK part of an URL when using mod_webkit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To use ModWebKit_, you'll usually have some lines like this in your
``http.conf``::
WKServer localhost 8086
SetHandler webkit-handler
Then /WK/Servlet will run Servlet.py in the default context.
The problem with this is that you now have an implementation dependent
fragment "WK" (standing for "WebKit_") in your application URLs. This
is considered bad for various reasons. Ideally, the URL should only
reflect the content and be human readable (you might want to read the
passage about `"The right URL for your link"`_ from the Tutorial `"The
Care and Feeding of Hyperlinks"`_ ). You cannot simply omit the
Location "/WK" or replace it by "/" if you want everything to be
served through WebKit_ (I don't know why, but it won't work). However,
you could replace "WK" by a memorable and reasonable keyword pointing
to the application itself, not the technology behind it.
.. _"The right URL for your link": http://www.webreference.com/html/tutorial25/3.html#HEAD-3
.. _"The Care and Feeding of Hyperlinks": http://www.webreference.com/html/tutorial25/
The more sophisticated solution is to map the URL using the URL
manipulation capabilities of Apache provided by mod_rewrite. Perhaps
you want to map different URLs to different contexts, or even to the
root of the website::
RewriteEngine_ On
RewriteRule_ ^(.*) /WK/LocalSite_/$1 [L,PT] ## NOTE: put no space between L and PT!
The L flag means "this the LAST rule, don't apply any more" and PT
means "PASS-THROUGH the changed URI to the next handler." You can
leave the L off, but the PT is essential if you are using mod_webkit.
See the mod_rewrite reference guide for an explanation.
To redirect all images to a static location, so that there's no
overhead from calling Webware, you would put this rewrite directive
*before* the above::
RewriteRule_ ^(.*\.(jpg|gif|png))$ /images/$1 [L,PT]
For a virtual domain, do something like::
...
RewriteEngine On
RewriteRule ^(.*)$ /WK/BobsYourUncle/$1 [L,PT]
This assumes ``/WK`` (not virtual) is where the AppServer is located,
and that you've created a context ``BobsYourUncle`` for that virtual
domain.
What if you aren't using mod_webkit? Say you are using a CGI adapter
at http://mainhost.com/cgi-bin/WebKit.cgi ? Then just replace ``/WK``
with =/cgi-bin/WebKit.cgi`` in all these examples. You can use
``[L]`` instead of ``[L,PT]`` if you aren't using mod_webkit. (Do you
need ``[L,PT]`` if you are using mod_snake, etc?)
-- IanBicking_ ... with changes by TavisRudd_ - 05 Mar 2002
If the image location is the same as the original URL, use a hyphen as
the second argument. You don't need $1's ()s in this case::
RewriteRule_ ^.*\.(jpg|gif|png)$ - [L,PT]
-- MikeOrr_ - 10 Dec 2001
I had two problems. The first was that I put my httpd.conf rewrite
rules in a such as / or the doc root. This gave either a
repeating path that was HTTP Forbidden, or gave no response. The
solution was to put the rules above the first .
The second problem was that if I put a slash in front of $1, the path
could not be found.
Finally, I was able to remove the ^ and $, which aren't needed to
capture the whole path. My final solution was::
...
DocumentRoot_ "/usr/local/apache/htdocs"
RewriteEngine_ On
RewriteRule_ (.*) /webkit/ContextName_$1 [L,PT]
...
I used Apache 1.3.22 (built from source) on Mandrake Linux 8.1.
-- ChuckEsterbrook_ - 22 Jan 2002
I used .htaccess files to set the rewrite rule, and had the following
rules as above::
RewriteEngine_ On
RewriteRule_ ^(.*\.(jpg|jpeg|gif|png)) /images/$1 [L,PT]
RewriteRule_ ^(.*) /cgi/OneShot_.cgi/MyContext_/$1 [L,PT]
This caused the recursion mentioned above in another case, to solve
this I went to /cgi/.htaccess and placed there::
RewriteEngine_ Off
This solved it. Now I can get to actually try to build my application.
-- BaruchEven_ - 17 Feb 2002
To clarify, if you want Webware to serve URLs in the top-level
directory (/index.py) without a /WW prefix *AND* serve certain files
statically::
1 RewriteRule_ ^/images(.*) - [L]
2 RewriteRule_ ^/pix/(images|thumbs)/(.*) - [L]
3 RewriteRule_ ^/WW($|/.*) - [L]
4 RewriteRule_ ^(.*) /WW/$1 [L,PT]
(The line numbers are not in the Apache file.)
(1) and (2) rewrite /images, /pix/images and /pix/thumbs to their
original URLs ("-") and use "L" to prevent rule 4 from executing.
(3) rewrites URLs with the WebKit_ prefix to their original URLs, and
uses "L" to prevent 4 from executing, which would wrongly rewrite
/WW/index.py to /WW/WW/index.py
(4) does most of the work, rewriting /index.py to /WW/index.py so that
users don't have to type the /WW prefix. "PT" is necessary or the
request won't be passed properly to WebKit.
I don't know if it's possible to make WebKit_ serve directly from /
using *AND* serve some files statically. If it were
possible, you'd need to supercede the "SetHandler webkit-handler",
thus::
WKServer localhost 8086
SetHandler_ webkit-handler
SetHandler_ apache-core-default-handler
but I don't know how you set the handler back to the default once
you've set it to something else.
-- MikeOrr_ - 16 Apr 2002
Hiding the /WK using ModPython in a Virtual Host
------------------------------------------------
My server is hosting 3 unrelated URLs. Each URL resolves to a
virtual host. One of these virtual hosts is used as the example.
The Apache config for the Virtual Server includes both the Location
directive as well as the RewriteRule_ directives. This allows for
different configs for each of the three virtual hosts.
The RewriteRules_ accomplish two things.
One, the third rule adds the "/wk" to the start of the URL passed
to Apache. This makes Webware happy. However, the first rule
ensures that a "/wk" does not already exist in the URL, as two
"/wk" values would be a problem.
Two, the second rule sends request for images used by mywebcontext
to the proper non-context directory. The "[L,PT]" on both the
second and third rules allows only one of those rules to be processed.
Given the server directory structure::
/pub
/httpd
/myweb
/mywebcontext
/mywebimages
The Webware context is::
'Contexts': {'mywebcontext':'/pub/httpd/myweb/mywebcontext',
'default':'mywebcontext'}
The Apache config is::
DocumentRoot_ /pub/httpd/myweb
ServerName_ www.e-myweb.com
#
# Added For WebWare_ / WebKit_ Support
#
RewriteEngine_ On
RewriteRule_ ^/wk/(.*) /$1 [R]
RewriteRule_ ^/images/(.*) /mywebimages/$1 [L,PT]
RewriteRule_ ^/(.*) /wk/$1 [L,PT]
SetHandler_ python-program
# add the directory that contains ModPythonAdapter_.py
PythonPath_ "sys.path+['/usr/local/Webware/WebKit_']"
PythonOption_ AppWorkDir_ /pub/httpd/myweb
PythonHandler_ ModPythonAdapter_
PythonDebug_ On
-- TacticalJack_ - 04 Sep 2002