extract only some chars

Discuss practical ways rearrange URLs using mod_rewrite.

extract only some chars

Postby danka » Tue Oct 14, 2008 2:14 am

Hi there,
I'm looking for a rewrite rule which extract some char in the url to create the right directory path.

For example:
from: www.foo.com/123/456/post-title/
to: /static/3/2/1/123/456.html

I know this is possible using VirtualDocumentRoot but I can't found similar expression in RewriteRule or RewriteCond

VirtualDocumentRoot /my/path/%2+/%1.-1/%1.-2/%1.-3/%1/doc_root

I need something like .-1, .-2, .-3 to extract that chars

Is it possible?
Thanks
danka
 
Posts: 3
Joined: Tue Oct 14, 2008 2:01 am

Postby richardk » Tue Oct 14, 2008 9:51 am

As far as i know you can only use 1 to 9.

You could match them individually, though.
Code: Select all
^(.*(.)(.)(.))$

for abcd, $1 = abcd, $2 = b, $3 = c and $4 = d.

Is 123 always three characters? Can there be less? What would happen then? What would happen to more than three?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby danka » Thu Oct 16, 2008 5:26 am

I need always three subdirectories, so if 123 is less than three characters a 0 (or underscore) replace the missing character and if 123 is more than three characters I use only last three

For example:
from: www.foo.com/12/456/post-title/
to: /static/2/1/0/12/456.html

from: www.foo.com/123456769/456/post-title/
to: /static/9/8/7/123456769/456.html

We have about 300.000 html files and we'd like to split the directories in this way as apache suggest for document root (http://httpd.apache.org/docs/2.2/mod/mo ... alias.html) but seems impossible to do this with rewriterule and rewritecond
Someone tell me to write an additional apache module to do this. We're thinking about this solution.

Waiting a better solution we write a php file that do this

Thanks
(Sorry for my english)
danka
 
Posts: 3
Joined: Tue Oct 14, 2008 2:01 am

Postby richardk » Thu Oct 16, 2008 7:03 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([0-9]*([0-9]+)([0-9]+)([0-9]+))/([0-9]+)/[^/]+/?$ /static/%4/%3/%2/%1/%5.html [QSA,L]
RewriteRule ^(([0-9]+)([0-9]+))/([0-9]+)/[^/]+/?$               /static/%3/%2/_/%1/%4.html  [QSA,L]
RewriteRule ^(([0-9]+))/([0-9]+)/[^/]+/?$                       /static/%2/_/_/%1/%3.html   [QSA,L]

I have used underscores, but you can replace them with 0.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby danka » Tue Oct 21, 2008 2:41 am

Many thanks richardk,
Unfortunately now I can't try it because using php we modify our code and now with rewrite it doesn't work.
As soon as I have the chance, I'll do it.
danka
 
Posts: 3
Joined: Tue Oct 14, 2008 2:01 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 19 guests

cron