httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sacha Varma <sa...@ssl.co.uk>
Subject [users@httpd] RewriteMap & hashes: dealing with a hash miss
Date Fri, 06 Feb 2009 16:05:15 GMT
(In the course of writing this e-mail I worked out the answer, but I 
thought I'd post it anyway, for future hapless rewriters such as myself.)

----

I have a website whose URLs often include an identifier for the 
underlying database record that powers the page.

Simplified example for illustrative purposes:

   /id/ABC123

The website has been redesigned, and is moving to a new domain, and all 
  the URLs have changed. The ids of the database records are preserved, 
  but not all the database records will exist on the new site.

I would like to rewrite URLs on the old site such that visitors to the 
old URL are redirected via an HTTP 301 (moved permanently) response to 
the new site.

I created an external DBM hash of ids to the new URLs, and configured 
Apache (2.2.0) as follows:

   RewriteMap ids dbm:/path/to/map.dbm
   RewriteRule ^/id/(.*) ${ids:$1} [R=301]

This worked fine except in the case where the id did not exist in the 
hash. I tried this:

   RewriteRule ^(/id/(.*)) ${ids:$2|$1} [R=301]

but this gives an endless redirection loop in the case of a hash miss 
(because although the URL is unchanged, the R flag causes a redirect).

I think what I need is a RewriteCond that checks whether the id is in 
the hash, but I don't know whether it's possible to express this.

----

The answer:

   RewriteCond %{REQUEST_URI} ^/id/(.*)
   RewriteCond ${ids:%1} !^$
   RewriteRule ^/id/(.*)$ ${ids:$1} [R=301]

It's a bit filthy that the first RewriteCond and the pattern in the 
RewriteRule are duplicated, but I don't know if there's a way to improve 
that.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Mime
View raw message