Forum Moderators: phranque
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!
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]
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]
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]
See the documentation [httpd.apache.org] of the RewriteCond server variables and the RewriteRule flags for more info.
Jim
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!
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