Forum Moderators: phranque

Message Too Old, No Replies

Retain HTTP queries in Apache Rewrite

         

JAB Creations

12:17 am on Jul 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm fixing up the URL's of my control panel and want to keep the HTTP queries. The problem is currently with the following URL...
admin/cms/edit?section=exam_sect&page=exam_page

Using...
RewriteRule ^(.*)$ index.php?q=$1 [L]


print_r($_GET);


I get...
Array ([q] => cms/edit)


When I want...
Array ([section] => exam_sect [page] => exam_page)


---------------

So I came across this example...

RewriteRule ^(.*)/(.*)/(.*)/(.*)$ shop.cgi?$1=$2&$3=$4

This rule rewrites URL:
< http://www.example.com/cat/cars/product/bmw >

into:
< http://www.example.com/shop.cgi?cat=cars&product=bmw >


That repetition makes it very easy to understand how variables are assigned (math books in grade school did the opposite making it difficult to find patterns with helpful repetition).

So for a while I experimented and somehow came up with the following that works...

RewriteRule ^(.*)/(.*)$ index.php


So now this URL...
admin/cms/edit?section=exam_sect&page=exam_page

Generates this array with print_r($_GET);...
Array ([section] => exam_sect [page] => exam_page)


For what I need at the moment at least it works though I'm a bit apprehensive since I'm not using ? and $1 at the end of the line nor in the middle have I (at least consciously) made a regular expression for ? (which I think might be \? to escape the question mark in the URL) to retain the HTTP query. Am I not far off, spot on or is this overly coincidental that this works? I understand things best with repetition (even if the need that sparked the question does not require it). In example with JavaScript years ago I saw someone use .parent.parent.parent which led me to figuring out you could access a nested frame using .parent.parent.

- John

g1smd

12:26 am on Jul 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The (.*) pattern means "match all of the input to the very end". You can't have more than one of these in a pattern, as you can't match "all" and then match "all" again.

Never use (.*) unless it is the last item in the pattern and is therefore logically followed by the $ end anchor.

To your original question, all you needed was the [QSA,L] flags and your very first rule.

JAB Creations

12:49 am on Jul 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Go figure it would be something I might have picked out if I was going through the flag documentation. It works great and I appreciate you pointing out the double .* broken logic, thanks!

- John

g1smd

12:56 am on Jul 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This configuration code is concise and powerful stuff. Often the most complex of things are achieved in an instruction of just a few characters.