You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Valentin Guggiana <va...@guggiana.ch> on 2004/08/26 13:54:41 UTC

[users@httpd] Stripping trailing numbers with mod_rewrite

Hello

I want to rewrite my URLs depending on username without
trailing numbers, e.g. with REMOTE_USER = user12345 :

/doc/logo.gif  -->  /doc/user/logo.gif


The rewrite rule for user dependency is quite simple and
works well (1):

RewriteCond %{REQUEST_URI} ^/doc/(.*)
RewriteCond %{LA-U:REMOTE_USER} .+
RewriteRule ^/doc/(.*) /doc/%{LA-U:REMOTE_USER}/$1 [NS]

For the above example /doc/logo.gif gets rewritten to
/doc/user12345/logo.gif. Not exactly what I want.


Stripping trailing numbers can easily be done with (2):

RewriteRule ^/doc/(.*)[0-9]/(.*) /doc/$1/$2 [N,NS]

/doc/user12345/logo.gif gets rewritten to /doc/user/logo.gif


But now: how to combine both? The following generates a loop
adding the username serveral times, so it's unusable (3):

RewriteCond %{REQUEST_URI} ^/doc/(.*)
RewriteCond %{LA-U:REMOTE_USER} .+
RewriteRule ^/doc/(.*) /doc/%{LA-U:REMOTE_USER}/$1 [C,NS]
RewriteRule ^/doc/(.*)[0-9]/(.*) /doc/$1/$2 [N,NS]


The next one does not catch the numbers (WHY ?), so it only
does the user dependency (4):

RewriteCond %{REQUEST_URI} ^/doc/(.*)
RewriteCond %{LA-U:REMOTE_USER} .+
RewriteRule ^/doc/(.*) /doc/%{LA-U:REMOTE_USER}/$1 [C,NS]
RewriteRule ^/doc/(.*)([0-9]*)/(.*) /doc/$1/$3 [L,NS]


The only rule set that works for a limited (but adaptable)
length of numbers is (5):

RewriteCond %{REQUEST_URI} ^/doc/(.*)
RewriteCond %{LA-U:REMOTE_USER} .+
RewriteRule ^/doc/(.*) /doc/%{LA-U:REMOTE_USER}/$1 [C,NS]
RewriteRule ^/doc/(.*)[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/(.*) /doc/$1/$2 [NS]
RewriteRule ^/doc/(.*)[0-9][0-9][0-9][0-9]/(.*) /doc/$1/$2 [NS]
RewriteRule ^/doc/(.*)[0-9][0-9]/(.*) /doc/$1/$2 [NS]
RewriteRule ^/doc/(.*)[0-9]/(.*) /doc/$1/$2 [L,NS]

It works fine for up to 15 traling numbers. If 7 are enough
one can remove the 4th line.


Is there a better way to do that or can somebody enlight me
why (4) does not work?



Thanks in advance,

Valentin

---------------------------------------------------------------------
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


Re: [users@httpd] Stripping trailing numbers with mod_rewrite

Posted by Valentin Guggiana <va...@guggiana.ch>.
Robert Andersson wrote:
> Valentin Guggiana wrote:
> 
>>I want to rewrite my URLs depending on username without
>>trailing numbers, e.g. with REMOTE_USER = user12345 :
>>
>>/doc/logo.gif  -->  /doc/user/logo.gif
> 
> 
> This may work:
> 
>     RewriteCond %{REQUEST_URI} ^/doc/(.*)
>     RewriteCond %{LA-U:REMOTE_USER} ^(.+?)\d*$
>     RewriteRule ^/doc/(.*) /doc/%1/$1 [NS]
> 
> Here you capture the username without trailing numbers in the condition, and
> then use it in the rule.
> 
> If the username cannot have numbers in them, except the trailing ones (like
> 1337user12345), you should change the second condition's pattern to
> "^([^\d]+)". Do use this pattern if you can.
> 
> If you have problems with the above, please explain the exact rules for how
> a valid username can be constructed.

That's exactly what I want! I'm new (started yesterday) in using mod_rewrite
and regular expressions. I missed back-references to RewriteCond and \d
for matching number.


Thanks for your help,

Valentin Guggiana

---------------------------------------------------------------------
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


Re: [users@httpd] Stripping trailing numbers with mod_rewrite

Posted by Robert Andersson <ro...@profundis.nu>.
Valentin Guggiana wrote:
> I want to rewrite my URLs depending on username without
> trailing numbers, e.g. with REMOTE_USER = user12345 :
>
> /doc/logo.gif  -->  /doc/user/logo.gif

This may work:

    RewriteCond %{REQUEST_URI} ^/doc/(.*)
    RewriteCond %{LA-U:REMOTE_USER} ^(.+?)\d*$
    RewriteRule ^/doc/(.*) /doc/%1/$1 [NS]

Here you capture the username without trailing numbers in the condition, and
then use it in the rule.

If the username cannot have numbers in them, except the trailing ones (like
1337user12345), you should change the second condition's pattern to
"^([^\d]+)". Do use this pattern if you can.

If you have problems with the above, please explain the exact rules for how
a valid username can be constructed.

Regards,
Robert Andersson


---------------------------------------------------------------------
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