capturing URL "pieces" in reverse for mod_rewrite
Moderator: General Moderators
capturing URL "pieces" in reverse for mod_rewrite
Does anyone know how to do this, or if this is even possible.
In order to utilize mod_rewrite correctly for my site, I'd like to be able to capture only the last two parts of an entered URL for use as values in a query string in the translated URL.
For Example:
if the user enters: http://www.mysite.com/piece_1/piece_2/piece_3/
I only want to capture piece_2 and piece_3 in my regular expression for my rewrite rule.
----------
if the user enters: http://www.mysite.com/piece_1/piece_2/piece_3/piece_4/
I only want piece_3 and piece_4
Is this at all possible? Or do I need to set up a new rewrite rule for every level of depth in the entered URL?
P.S. I know this sounds like an odd request, but rest assured that I have my reasons.
In order to utilize mod_rewrite correctly for my site, I'd like to be able to capture only the last two parts of an entered URL for use as values in a query string in the translated URL.
For Example:
if the user enters: http://www.mysite.com/piece_1/piece_2/piece_3/
I only want to capture piece_2 and piece_3 in my regular expression for my rewrite rule.
----------
if the user enters: http://www.mysite.com/piece_1/piece_2/piece_3/piece_4/
I only want piece_3 and piece_4
Is this at all possible? Or do I need to set up a new rewrite rule for every level of depth in the entered URL?
P.S. I know this sounds like an odd request, but rest assured that I have my reasons.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Had to use 3 rules to catch everything:
The tester.php file referenced in the rules is just something I put together for debugging to show me that the querystrings were coming through OK.
Code: Select all
RewriteRule ^([^/]*)/?$ tester.php?page=$1 [QSA,L]
RewriteRule ^([^/]*)/([^/]*)/?$ tester.php?page=$2&parent=$1 [QSA,L]
RewriteRule ^.*/([^/]*)/([^/]*)/?$ tester.php?page=$2&parent=$1 [QSA,L]- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Take a close look at the third rule. It may not do what you expect it to. The matching before the first '/' slash is $1, the next match is $2. That means that you are not using the third match at all in your rewrite rule.snailface wrote:Had to use 3 rules to catch everything:
The tester.php file referenced in the rules is just something I put together for debugging to show me that the querystrings were coming through OK.Code: Select all
RewriteRule ^([^/]*)/?$ tester.php?page=$1 [QSA,L] RewriteRule ^([^/]*)/([^/]*)/?$ tester.php?page=$2&parent=$1 [QSA,L] RewriteRule ^.*/([^/]*)/([^/]*)/?$ tester.php?page=$2&parent=$1 [QSA,L]
Everah,
My understanding of mod_rewrite is that the substrings within parens in the Regular Expression are what is captured. Hence the match before the first slash is not captured and $1 is the string matching the expression in the first set of parens.
I checked it all again, and it is behaving as I expect it too.
However, thanks to you, when I tested the 3rd rule, I did notice some odd behavior:
Look at the rule again:
The odd behavior I noticed was running a url such as: "www.mysite.com/piece1/piece2/piece3/".
My expectation was that this URL would be re-written as: "www.mysite.com/tester?page=piece3&parent=piece2". But what I got was: "www.mysite.com/tester?page=piece3&parent="
Looking at the rule further, I found the error to be that last *. That combined with the specification in the regular expression that the URL should end with either one slash or no slash was causing the rule to capture the nothingness after the URL as a value.
Hence I rewrote the rule (extending the change to all 3):
* * * * *
Ambush Commander,
I could have done it with PHP, but after I started working on it through mod-rewrite, I got hung up on solving the problem. I wasn't going to stop until I figured it out.
My understanding of mod_rewrite is that the substrings within parens in the Regular Expression are what is captured. Hence the match before the first slash is not captured and $1 is the string matching the expression in the first set of parens.
I checked it all again, and it is behaving as I expect it too.
However, thanks to you, when I tested the 3rd rule, I did notice some odd behavior:
Look at the rule again:
Code: Select all
RewriteRule ^.*/([^/]*)/([^/]*)/?$ tester.php?page=$2&parent=$1 [QSA,L]My expectation was that this URL would be re-written as: "www.mysite.com/tester?page=piece3&parent=piece2". But what I got was: "www.mysite.com/tester?page=piece3&parent="
Looking at the rule further, I found the error to be that last *. That combined with the specification in the regular expression that the URL should end with either one slash or no slash was causing the rule to capture the nothingness after the URL as a value.
Hence I rewrote the rule (extending the change to all 3):
Code: Select all
RewriteRule ^([^/]+)/?$ tester.php?page=$1 [QSA,L]
RewriteRule ^([^/]+)/([^/]+)/?$ tester.php?page=$2&parent=$1 [QSA,L]
RewriteRule ^.*/([^/]+)/([^/]+)/?$ tester.php?page=$2&parent=$1 [QSA,L]Ambush Commander,
I could have done it with PHP, but after I started working on it through mod-rewrite, I got hung up on solving the problem. I wasn't going to stop until I figured it out.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
fastfingertips
- Forum Contributor
- Posts: 242
- Joined: Sun Dec 28, 2003 1:40 am
- Contact:
Better is to simply send all the text after your domain name to a front controller and simply handle the parameters through PHP. In this way you will achieve the freedon you'll need if you are planning expansions etc.
I've also used your system in my earlier application but it became very hard to handle all custom implementations.
I've also used your system in my earlier application but it became very hard to handle all custom implementations.