Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite match all?

         

zRonin

12:36 am on Jul 27, 2006 (gmt 0)

10+ Year Member



I would like to catch everything in a certain directory and redirect it one directory above. Is there a simpler way to do this than the code I am using?

RewriteRule ^downloads/c([^-]+)-(.*)/(.*)/(.*)/(.*)$ $3/$4/$5?%{QUERY_STRING} [L]
RewriteRule ^downloads/c([^-]+)-(.*)/(.*)/(.*)$ $3/$4?%{QUERY_STRING} [L]
RewriteRule ^downloads/c([^-]+)-(.*)/(.*)$ $3?%{QUERY_STRING} [L]

I wanted to just do (.*)$ but unfortunately it doesnt look like mod_rewrite will look past / when matching. I also tried using ((.*)¦/) but that wont work because both parethesis return results and I cant tell which $n is which.

[edited by: zRonin at 12:36 am (utc) on July 27, 2006]

jdMorgan

1:40 am on Jul 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mod_rewrite has no problems 'matching past slashes', although you may be seeing a side-effect of the 'greediness' of the ".*" pattern.

I would suggest:


RewriteRule ^downloads/c[^\-]+-[^/]+/(.+)$ downloads/$1 [L]

That is, "Match 'downloads/c' followed by one or more characters not equal to a literal hyphen, followed by a hyphen, followed by one or more characters not equal to a slash, followed by a slash, and ending with one or more of any characters." Only this last group needs to be parenthesized in order to create a back-reference ($1).

This should handle any number of subdirectories in /downloads, removing the "/c<something>-<something>/ and retaining the rest; the query string should be passed through unchanged by default (you don't need to include it in the rule).

> I also tried using ((.*)¦/) but that wont work because both parethesis return results and I cant tell which $n is which.

That expression is ill-formed, but let's use ((ab)/cd) as a simple example. In order to resolve nested parentheses, count left parentheses; $1 would contain the whole matching string (ab/cd), while $2 would contain only the first part (ab).

If the above isn't what you need, please post an example with both an 'existing' and 'desired' complete local URL-path.

Jim

zRonin

11:32 pm on Jul 28, 2006 (gmt 0)

10+ Year Member



Thanks for your reply. It was very thorough and you explained everything well.