Web Servers on Unix and Linux - mod rewrite - the basics

This is Interesting: Free IT Magazines  
Home > Archive > Web Servers on Unix and Linux > June 2004 > mod rewrite - the basics





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author mod rewrite - the basics
Chris Challis

2004-06-01, 5:03 pm

I've just read through the mod rewrite URL Rewriting Engine helpfiles
and my first comment is "wow".

I considered myself quite a technical person until I read that. :-(

Having read through and understood (I think) most of the RewriteRule
directive I have one question unanswered.

Why the need for $1 in the substitutions section.

RewriteRule ^/somepath(.*) /otherpath$1

results in a URL of something like /otherpath/pathinfo

What is the $1 doing? $ is seemingly an end of line marker. What
does the 1 do? Can I use other integeres there?
Chris Morris

2004-06-01, 5:03 pm

challisc@hotmail.com (Chris Challis) writes:
> Why the need for $1 in the substitutions section.
>
> RewriteRule ^/somepath(.*) /otherpath$1
>
> results in a URL of something like /otherpath/pathinfo
>
> What is the $1 doing? $ is seemingly an end of line marker. What
> does the 1 do? Can I use other integeres there?


'$' is end of line
'$1' is contents of first sub-expression - the '(.*)' in the source.

So /somepath/foo -> /otherpath/foo ($1 = '/foo')
/somepaths -> /otherpaths ($1 = 's')

You can use more
RewriteRule ^/([a-z]+)/([a-z]+)/ /$2/$1/
does
/a/b/ -> /b/a/ ($1 = 'a', $2 = 'b')
/part/sub/ -> /sub/part/ ($1 = 'part', $2 = 'sub')
/a/20/ -> /a/20/ (pattern doesn't match)

Look for a regular expressions tutorial.

--
Chris
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com