httpd-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tim Burden" <...@Burden.ca>
Subject Re: [users@httpd] Conditional RewriteRule
Date Mon, 24 May 2004 17:09:55 GMT
What about writing one rule to do the zero-padding, then pass that through
to a much simpler rule to translate 6-digit numbers on to the file system?

However even the problem of doing zero padding in regex seems non-trivial.
You'll probably have to cascade it in some way anyway.

----- Original Message ----- 
From: "Rick" <listx-apache@dstudio.clara.net>
To: <users@httpd.apache.org>
Sent: Monday, May 24, 2004 1:15 AM
Subject: [users@httpd] Conditional RewriteRule


> Good Morning Apache Users,
>
> This is a question about using conditions in mod_rewrite.
> Here is the background ...
>
> * We have a large, and growing, number of pages derived from an external
>   database which we have stored in a hashed directory structure.
>
> * The pages all have filenames of the form w<ID_NUMBER>.html, eg:
>   w1234.html, w12345.html
>
> * The highest id number, not yet reached, will be 999999. We started with
a
>   4-digit number, so all the id numbers will have either 4, 5 or 6 digits.
>   It may be years before we get to the 6 digit range, so in practice right
>   now they have either 4 or 5 digits.
>
> * The directory in which pages are stored can be found by padding the id
>   number to 6 digits, then taking the first four as a directory tree, eg:
>     - the path to w1234.html is /widgets/0/0/1/2/1234.html
>     - the path to w12345.html is /widgets/0/1/2/3/12345.html
>   This was designed to be filesystem-friendly, with a maximum of 100 pages
>   per directory.
>
> But our canonical URLs must be of the form /widgets/<ID_NUMBER>.html
> eg: we need to rewrite /widgets/1234.html to /widgets/0/0/1/2/1234.html
>
> My problem is how to formulate an *efficient* regular expression that will
> perform the substitution. I'm really having a hard time writing a
> RewriteRule regex that does not look embarassing.
>
> Seeems to me there are two possible logical approaches: match the numeric
> part of the filename, then act conditionally on
> * the number of digits: is it 4 or 5?
> * the value of the number: is it greater than 9999 ?
>
> But I can't figure out how to do that in either RewriteRule or
RewriteCond.
> The best I can think of so far is below. The first RewriteRule matches
pages
> with 5 digits, and the second matches those with 4 digits.
>
> RewriteEngine On
> RewriteBase   /widgets
> RewriteRule ^.*([0-9])([0-9])([0-9])([0-9]{2})\.html$
/widgets/0/$1/$2/$3/w$1$2$3$4.html [L]
> RewriteRule ^.*([0-9])([0-9])([0-9])([0-9])\.html$
/widgets/0/0/$1/$2/w$1$2$3$4.html [L]
>
> It works, but pages with 4 digits have to be processed twice. There *must*
> be a better way. Can anyone point me in the right direction? Perhaps some
> way of getting the digits into environment variables?
>
> In this case the right direction might include changing the directory
> structure to make it more RewriteRule-friendly. Any suggestions would be
> much appreciated.
>
>
>
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Mime
View raw message