[thelist] mod rewriting question

Tom Dell'Aringa pixelmech at gmail.com
Tue May 15 14:25:40 CDT 2007


On 5/15/07, Stephen Rider <evolt_org at striderweb.com> wrote:
>
>
> 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]
>
> 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.


Thanks, I'll give that a shot. I do know some regEx, just not sure how it
all fits into the rewrite syntax. You threw me with /archives as well - I'm
not using that term, but 'articles' - easy oversight. I'll change that and
give it a go.

Tom



More information about the thelist mailing list