views:

110

answers:

3

Hi,

I have an htaccess file which begins with the regular stuff:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

then has a few rewrites eg:

RewriteRule ^protect/?$ /Legal/Your-Financial-Protection.aspx [NC, L, R=301]

then ends with a rewritemap:

RewriteMap map txt:rewritemaps/map.txt  [NC]
RewriteRule ^(.+)$  ${map:$1} [R=301]

The rewritemap contains all of our legacy urls - pages on our current site and short urls that will be redirected to equivalent pages on the new site (there are about 4k, so I need to use a map really):

Inspiration.aspx /Inspiration.aspx
Destinations/Africa/Countries/Gabon.aspx /Destinations/Africa/Gabon.aspx
indonesia /Destinations/Southeast-Asia/Indonesia.aspx

The problem is, with the rewritemap enabled (ie not commented out), all my urls (even those not matched) redirect to / - including stylesheets, js, images etc.

What I'm after is uris that match the pattern in the map to redirect to the replacement and everything else to pass through (ie stay the same).

I've tried setting a default of $1 on the map:

RewriteRule ^(.+)$  ${map:$1|$1} [R=301]

but this just causes a redirect loop. I think I could also skip rewriting for all .css, .js, .jpg etc but would rather not have to maintain a list of used file extensions.

FYI, I'm using ISAPIRewrite from HeliconTech (as I'm on IIS 6) although it claims to handle rewritemaps as per apache and we've never had problems in the past.

Any help would be hugely appreciated!

Thanks, Adam

A: 

Just put only the pairs in the map that actually differ. Otherwise you will redirect to the same value and get an infinite recursion.

And to redirect only when a match is found, try this:

RewriteCond ${map:$1} ^/.+
RewriteRule ^(.+)$ %0 [R=301]
Gumbo
Thanks - that's solved the loop issue but it's still not matching anything in the map
adam
A: 

Gumbo's solution is great, except that it doesn't seem to handle URL's with spaces. http://www.webmasterworld.com/apache/3830228.htm offers insight into how to handle spaces, combining the two like so:

  RewriteCond ${moved:${escape:$1}} ^/.+
  RewriteRule ^(.+)$ %0 [R=301]

Doesn't work. Damn I hate mod_rewrite

  IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (2) init rewrite engine with requested uri /press/Partners With City.pdf
  IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (3) applying pattern '^(.+)$' to uri '/press/Partners With City.pdf'
  IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (5) map lookup OK: map=escape key=/press/Partners With City.pdf -> val=/press/Partners%20With%20City.pdf
  IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (6) cache lookup FAILED, forcing new map lookup
  IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (5) map lookup FAILED: map=moved[txt] key=/press/Partners%20With%20City.pdf
  IPADDR - - [07/Jan/2010:01:44:58 --0500] [example.org/sid#842a5a8][rid#100e0a18/initial] (4) RewriteCond: input='' pattern='^/.+' => not-matched
A: 

adam, Gumbo's rules includes the leading slash in the key, and the posted version of your map is missing them.

RewriteLogLevel 9 is very handy for uncovering things such as this.