[thelist] mod rewriting question

Stephen Rider evolt_org at striderweb.com
Tue May 15 11:38:11 CDT 2007


On May 15, 2007, at 10:31 AM, misterhaan wrote:

> Stephen Rider wrote:
>> RewriteRule ^(0-9){4}/(0-9){2}/(0-9){2}/(.+?)/?$ /archives/$4/
>> [R=301,L]
> does it work with parentheses?  i've always used brackets to match a
> range of characters:
>
> RewriteRule ^[0-9]{4}(/[0-9]{2}){2}/([^/]+)/?$ /archives/$2/ [R=301,L]

Tom:

Just FYI -- misterhaan is correct about the brackets -- in all the  
places where I have (0-9) it should have been [0-9].  That also  
changes the final $4 to $1

The other changes he makes are optional -- either version should  
work.  Like a language, grep has flexible enough "grammar" that there  
are often different way of saying the same thing. :)

My corrected version would be:
RewriteRule ^[0-9]{4}/[0-9]{2}/[0-9]{2}/(.+?)/?$ /archives/$1/ [R=301,L]

How it works:

^ = beginning of string
[0-9]{4}/ = any one of the digits 0 to 9, four times, followed by a  
slash
[0-9]{2}/  = same thing, but only two digits.  Do this twice.
(.+?) = any string of characters (parens capture it for later), until  
you hit...
/? = zero or one slashes, followed by...
$ = end of string

replacing with /archives/first-captured-string/

Good luck with it.  Regular Expressions (grep) are a good thing to  
learn.  They can be very handy when it comes time to do a search/ 
replace over an entire site's worth of files.

Stephen Rider
<http://striderweb.com/>



More information about the thelist mailing list