Forum Moderators: phranque

Message Too Old, No Replies

[beginner] mod_rewrite

Accessing parameters

         

tata668

2:19 am on Mar 28, 2005 (gmt 0)

10+ Year Member



Hi, I'm a real beginner using mod_rewrite...
I've read some documentation but didn't found the answer for my problem... Please help!

This is the line I test in httpd.conf

RewriteRule (.*) /test.php?test=$1

Am I crazy or $1 won't contain the original url parameters?

Let's say I call "aPage.php?param=1", I would expect the final url to be "/test.php?test=aPage.php?param=1" but in fact it's "/test.php?test=aPage.php".
The parameter "param=1" is not there!

How can I access the parameters using RewriteRule?

Thanks a lot for any help!

tata668

2:27 am on Mar 28, 2005 (gmt 0)

10+ Year Member



To be more clear...

I'd like:

/12/aPage.php?param=1

to become:

/aPage.php?param=1&id=12

Thus I need to keep the existing parameter and add a new one.

Is this possible?

jdMorgan

6:09 am on Mar 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can't directly access query string parameters in a RewriteRule.

If you need to test a query string parameter and/or create a back-reference to it, then use


RewriteCond %{QUERY_STRING} [i]<pattern>[/i]

preceding your rule, and back-reference that pattern using %1.

You didn't say which parts of your URL and query string were variable. Assuming that all are variable, then the following code would meet your stated requirements:


RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^/([0-9]+)/([a-z]+)\.php$ /$2.php?%1&id=$1 [NC,L]

The only fixed thing in the code is ".php", and the rule will match any URL that looks like "/<numbers>/<letters>.php" and has a non-blank query-string. You'll likely want to make it a bit more selective.

If you do not care what order the new parameter is appended in, you could use this simpler version:


RewriteRule ^/([0-9]+)/([a-z]+)\.php$ /$2.php?id=$1 [NC,QSA,L]

Here, the [QSA] flag tells the rule to append the new query string any pre-existing query string.

See the documentation [httpd.apache.org] of the RewriteCond server variables and the RewriteRule flags for more info.

Jim

tata668

3:51 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



Wow! That is a complete answer..

Thanks a lot jdMorgan!

tata668

4:19 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



Welll... Still one thing I'm not able to achieve!

Let's say I want to remove all the "lang" parameters from the original querystring before appending a new one.

Here's my real example:

RewriteCond %{QUERY_STRING} ^(.*)(lang=[^&]*)(.*)$ [NC]
RewriteRule (.*)/(frĶen)/(.*).php$ $1/$3.php?%1%3&lang=$2 [NC]

I want to strip any existing "lang" parameter and use the "fr" or "en" to create it from scratch. My current code works but only if there is one instance of the "lang" parameter.
What can I do to strip all possible "lang" parameters?
i.e. /mysite/en/allo.php?lang=esp&lang=fr

I have to replace all the matches in the querystring!

jdMorgan

5:04 pm on Mar 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a rather horrible problem. It calls for iteratively removing lang parms, which means you have to re-start your code using the [N] flag on RewriteRule if a second instance of lang is present... Terribly inefficient.

Can you prevent multiple lang parms from happening in the first place (i.e. in your script)?

Or can you place some upper bound on the number of possible lang parms? If so, that may allow a more efficient solution.

BTW, I'd suggest ([^&]+&)? instead of (.*) as the first pattern in your RewriteCond -- it is much faster to process than the 'greedy and promiscuous' ".*".

Jim

tata668

5:28 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



Thanks again for those extra informations.
I think I'm going to be able to get something I can live with!