capturing URL "pieces" in reverse for mod_rewrite

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
snailface
Forum Newbie
Posts: 7
Joined: Wed Jul 26, 2006 7:03 am

capturing URL "pieces" in reverse for mod_rewrite

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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).
snailface
Forum Newbie
Posts: 7
Joined: Wed Jul 26, 2006 7:03 am

Post by snailface »

I'm getting close to figuring it out. I'll post my results here when I'm done.
snailface
Forum Newbie
Posts: 7
Joined: Wed Jul 26, 2006 7:03 am

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Why not pass the whole path to PHP and use PHP to figure the parsing out?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
snailface
Forum Newbie
Posts: 7
Joined: Wed Jul 26, 2006 7:03 am

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Good job. Glad you got it all worked out.
snailface
Forum Newbie
Posts: 7
Joined: Wed Jul 26, 2006 7:03 am

Post by snailface »

Thanks. Might sound a little stupid. But I'm pretty proud. I love days like this when I reach a breakthrough.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post 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.
Post Reply