Welcome to WebmasterWorld Guest from 18.207.238.169

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

mod rewrite match all?

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

Junior Member

10+ Year Member

joined:June 7, 2005
posts:53
votes: 0


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]

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

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


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

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

Junior Member

10+ Year Member

joined:June 7, 2005
posts:53
votes: 0


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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members