[Hampshire] mod_rewrite guide

Top Page

Reply to this message
Author: Paul Tansom
Date:  
To: hampshire
Subject: [Hampshire] mod_rewrite guide
Can anyone point me in the direction of a *decent* mod_rewrite guide.
I've enphasised the word decent there because there are loads of guides
listed on Google, many of which are little more than slightly adjusted
versions of the one on the Apache website, and that guide is severely
lacking in clarity in some areas.

A few examples of things that are causing me some headaches at the
moment are:

The RewriteBase seems to be a bit of a moving target. If I'm using it
within the .htaccess file then it is relative to the root of the
website, whereas when used in the Apache configuration file it is
relative to the root of the file system.

The ordering of handlers is somewhat less than clear. If I put some
rules in the Apache config then they are never used, whereas if they are
in the .htaccess they work fine. This seems to be due to the fact that I
am trying to rewrite a URL of the form /cgi/page.pl?page=about . As soon
as Apache sees the /cgi/page.pl it passes through to the next handler
and completely skips mod_rewrite. I'm assuming this is due to the fact
that I have a ScriptAlias defined, and this seems to be confirmed by
removing it; when I do the rewrite works, but the web page delivery
fails since I still need to create it using the script (I'm just hiding
the true URL to make things more friendly).

So far I've not found any clear enough documentation to clarify some of
these issues, and it isn't helped by the fact that there's is some
caching between the browser and server that isn't always clearing with a
bounce of the server (needed to pick up config changes) and/or clearing
the browser cache (there's no proxy involved).

If anyone is intersted in more details read on...

This is working to redirect something of the form
/cgi/page.pl?page=about to /about when used within .htaccess , but not
within the Apache config.

RewriteBase /
RewriteCond %{QUERY_STRING} page=([a-z]+)
RewriteRule ^/?cgi/page\.pl$ /%1? [R=301,L]

This is managing to deliver the correct content from
/cgi/page.pl?page=about for /about when used within the Apache config. I
have to use RewriteBase / when used within .htaccess

RewriteBase /var/www/tudorsc
RewriteRule ^/?([a-z]+)$ /cgi/page.pl?page=$1 [L]

Of course this looks as though it will create a loop if used together,
so I've either got to slightly modify the first with:

RewriteCond %{QUERY_STRING} !apt

and adjust the second to use the rule:

RewriteRule ^/?([a-z]+)$ /cgi/page.pl?apt&page=$1 [L]

..or I'm wondering whether I may be able to make use of [PT] to pass
through to the next handler somehow in the second section..

Of course all this is great fun, so long as you don't end up going
rounnd in circles too much :)

--
Paul Tansom | Aptanet Ltd. | http://www.aptanet.com/ | 023 9238 0001
======================================================================
Registered in England | Company No: 4905028 | Registered Office:
Crawford House, Hambledon Road, Denmead, Waterlooville, Hants, PO7 6NU