Page 1 of 1
capturing URL "pieces" in reverse for mod_rewrite
Posted: Tue Aug 01, 2006 7:30 am
by snailface
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.
Posted: Tue Aug 01, 2006 9:22 am
by RobertGonzalez
You are going to need to set up a rule for each. I do not believe that apache can handle any type of 'figuring things out' within a .htaccess file (or even with the configuration file).
Posted: Fri Aug 11, 2006 10:10 am
by snailface
I'm getting close to figuring it out. I'll post my results here when I'm done.
Posted: Fri Aug 11, 2006 11:16 am
by snailface
Had to use 3 rules to catch everything:
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]
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.
Posted: Fri Aug 11, 2006 11:18 am
by Ambush Commander
Why not pass the whole path to PHP and use PHP to figure the parsing out?
Posted: Fri Aug 11, 2006 11:27 am
by RobertGonzalez
snailface wrote:Had to use 3 rules to catch everything:
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]
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.
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.
Posted: Fri Aug 11, 2006 2:41 pm
by snailface
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:
Code: Select all
RewriteRule ^.*/([^/]*)/([^/]*)/?$ tester.php?page=$2&parent=$1 [QSA,L]
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):
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.
Posted: Fri Aug 11, 2006 3:37 pm
by RobertGonzalez
Good job. Glad you got it all worked out.
Posted: Fri Aug 11, 2006 6:53 pm
by snailface
Thanks. Might sound a little stupid. But I'm pretty proud. I love days like this when I reach a breakthrough.
Posted: Mon Aug 14, 2006 3:38 am
by fastfingertips
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.