RewriteMap & hashes: dealing with a hash miss

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



(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@xxxxxxxxxxxxxxxx
  "   from the digest: users-digest-unsubscribe@xxxxxxxxxxxxxxxx
For additional commands, e-mail: users-help@xxxxxxxxxxxxxxxx


[Index of Archives]     [Open SSH Users]     [Linux ACPI]     [Linux Kernel]     [Linux Laptop]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Squid]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]

  Powered by Linux