Apache redirects with RewriteMap and RewriteCond 21

Posted by Peter Burkholder Wed, 15 Jul 2009 01:13:00 GMT

At work we’re migrating several thousand research articles from Zope to another CMS. The CMS folks are taking care of moving the content, but when we’re done we’re going to institute a boatload of R=301 redirects from the old URLs to the new URLs.

RewriteMap is the accepted way with mod_rewrite of handling a lot of one-to-one mappings that don’t follow any particular pattern. What I figured out today was that I can use a rewrite map in a RewriteCond statement so I only do the redirect when there’s a match in the rewrite map lookup.

Here are some snippets from my httpd.conf to illustrate:

For testing, we’ll want logging:


RewriteLog /var/log/httpd/rewrite.log
RewriteLogLevel 2

Define the map we’re using. The content of the map is ‘old_uri new_uri’ with a space separating the two. Use .txt for testing, and the below we’ll convert to a DBM.


RewriteMap research_map txt:/etc/httpd/conf/research_map.txt 

Here, I got a hint from http://www.tunnell.org/blog_posts_view.php?blog_postid=3. Remember that the syntax for a RewriteCond is:

RewriteCond TestString ConditionPattern

TestString will be a map lookup of $1, where $1 is the match string of the following RewriteRule, expressed: ${research_map:$1}

For ConditionPattern, we will test if the TestString is lexically greater than ””, the empty string, which is what the map lookup returns when there’s no match. Expressed: >""


RewriteCond ${research_map:$1}  >""     # IE, if map result is greater than "" 

So if the URL starts with /research, then use the research_map value for the key $1 to redirect to new address


RewriteRule ^(/research/.*$) ${research_map:$1}      [R=301,L]

What we end up with is a few lines of configuration that quickly let me put in place 3342 new redirects. Here’s the whole stanza:



RewriteMap research_map txt:/etc/httpd/conf/research_map.txt 
RewriteCond ${research_map:$1}  >""     # IE, if map result is greater than "" 
RewriteRule ^(/research/.*$) ${research_map:$1}      [R=301,L]

# Same thing, but lookup with a trailing slash if there isn't one
RewriteCond ${research_map:$1/}  >"" 
RewriteRule ^(/research/.*[^/]$) ${research_map:$1/}      [R=301,L]

Lastly, converting the textfile to a dbm speeds up the lookup by at least an order of magnitude.

Comments

Leave a comment

  1. JT Justman 27 days later:
    Perfect, just what I was looking for!
  2. fast cash payday loan about 1 year later:
    I have obtained fast cash payday loan from the convenience of my home and I did not have to stand in queue. The application process is easy and you do not have to wait long to get the approval. Just within 24 hours, you can get the money transferred into your bank account.
  3. Northam Accommodation about 1 year later:
    Thanks for that!
  4. Baby Gifts about 1 year later:
    Excellent. Thanks for the post.
  5. Promotional Pens about 1 year later:
    Thanks nice post
  6. Custom Builders about 1 year later:
    Thanks. This is great.
  7. Perth Builders about 1 year later:
    Thanks.
  8. Perth Builders about 1 year later:
    Thanks for the snippets.
  9. Visual Merchandising about 1 year later:
    Thanks for the post.
  10. WA South West about 1 year later:
    Thanks for the blog.
  11. website leasing about 1 year later:
    very well written blog
  12. bridesmaid dresses about 1 year later:
    Thank you for sharing these!Very useful!
  13. Low Carb Snacks about 1 year later:
    Eating low carb meals can assist you lose weight and feel healthier overall.
  14. darmowe pozycjonowanie about 1 year later:
    I agree with you
  15. krzyzowki about 1 year later:
    Great read You might want to follow up to this topic
  16. Fiji Resorts about 1 year later:
    Fantastic, looks like a really good program.
  17. Melbourne Builders about 1 year later:
    Thank you for the help. This is great.
  18. online contest about 1 year later:
    This is highly recommended post to me as I have learned something from this reading. I suggest my friends to visit this blog. Great job!!!
  19. darmowe programy about 1 year later:
    I am not sure where you are getting your information, but great topic
  20. Diablo 3 over 2 years later:
    no kidding,this is a very good blog post,i like the idea how you organize your points of view by authority then you speak about each individually,very smart of you,consider having a new blog reader
  21. The Diet Solution Program over 2 years later:
    Cloudsourcing combines on-demand business process outsourcing (BPO) with crowdsourcing technologies to enable companies to purchase quality BPO services on-demand through a pay-per-use model.
Comments