Apache rewrite rule with back-reference in the regex

This might be obvious, but it took me a little while to work it out so I’m documenting it here for posterity.

I recently rearranged some of the files in the PyMOTW section of my site, renaming PyMOTW/atexit/atexit.html to PyMOTW/atexit/index.html for example. I wanted to create an Apache RewriteRule to fix all of the references to those pages, but didn’t want to list them all manually.

I ended up with this rule:

RewriteRule ^/PyMOTW/(.*)/1.html$ /PyMOTW/$1/ [L,R=permanent]

The 1, is what I had trouble with. I kept trying variations of $1 because, well, that’s what I used in the second part of the rule. Oh, well.