1

I am dealing with an API that is accessing an URL on my website and adding
/?parameter1=value&parameter2=value to the url. I want my htaccess to handle this request and only keep the parameter's values. The API is also adding many other parameters in the query string but I just want to keep two of them.

The following does not work:

RewriteCond %{QUERY_STRING} ^parameter1=([^&]+)
RewriteCond %{QUERY_STRING} ^parameter2=([^&]+)
RewriteRule ^my-url/?$ controller.php?parameter1=%1&parameter2=%2

How can I do that correctly?

EDIT:

Here is an example.

The url is:
http://example.com/my-url/?parameter1=value1&stuff=1&stuff2=2&parameter2=value2

The htaccess should get the parameter1 & parameter2 values.

10
  • Can you give an example of what you are trying to achieve? A before url and after url? Commented Mar 31, 2014 at 22:42
  • Howlin: Example added. Commented Mar 31, 2014 at 22:49
  • You can just use the QSA flag and just get the two parameters you want in your code. Commented Mar 31, 2014 at 22:49
  • 2
    The reason your original redirect doesn't work is that RewriteCond backreferences only work for the last RewriteCond statement. So it would work for parameter2 in your example, but not parameter1. Commented Mar 31, 2014 at 23:21
  • 2
    @PeterLur That doesn't matter, it doesn't make your code any slower or faster. All you have to do is just get the value directly for the two you want. $_GET["parameter1"] and $_GET["parameter2"]. Doesn't matter if there are others in array. It's no different than $_SERVER var, which contains many parameters that you may or may not use. It's just there. Commented Mar 31, 2014 at 23:21

3 Answers 3

2

Try this and just grab the two parameters in your server side code. e.g. $_GET. If they are always in the query string, you can just check for parameter1 and then it will append the other parameters and you can get what you need.

 RewriteCond %{QUERY_STRING} ^\bparameter1=
 RewriteRule ^my-url/?$ controller.php [QSA,L]
Sign up to request clarification or add additional context in comments.

3 Comments

Could you please let me know what the [QSA,L] does exactly? I don't understand why the parameters are being appended to the Rewrite. It works but I would like to know why.
Nevermind found it: QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite. Thanks!
The QSA flag stands for QUERY STRING APPEND.. This will explain it better for you httpd.apache.org/docs/2.2/rewrite/flags.html#flag_qsa. It is a very helpful flag when dealing with query strings.
0

Try the following:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /my-url/\?parameter1=(.*)&stuff=(.*)&stuff2=(.*)&parameter2=(.*)\ HTTP
RewriteRule ^ /controller.php?parameter1=%2&parameter2=%5\? [R,L]

Mind it will only get the two parameters if there are always &stuff=1&stuff2=2 those two values in the url.

1 Comment

Howlin, there are not always the same parameters. The thing I know is that parameter1 and parameter2 will be there.
0

I've had to do a similar setup with key/value pairs in the GET which could come in any order depending on the client platform.

Based on useful pointers in this thread, this is what I came up with based on the question above:

#= 1. Catch parameter1, append querystring as "p1=". Rewrite to "/parameter1" below:
RewriteCond %{REQUEST_URI}  ^/my-url/$
RewriteCond %{QUERY_STRING} ^parameter1=([^&]+)
RewriteRule (.*) /parameter1?%{QUERY_STRING}&p1=%1 [P,QSD]

#= 2. Catch parameter2, append querystring as "p2=". Rewrite to "/parameter2" below:
RewriteCond %{REQUEST_URI}  ^/parameter1$
RewriteCond %{QUERY_STRING} ^parameter2=([^&]+)
RewriteRule (.*) /parameter2?%{QUERY_STRING}&p2=%1 [P,QSD]

#= 3. Now explicitly catch p1,p2 and feed to them to the back-end url:
RewriteCond %{REQUEST_URI}  ^/parameter2$
RewriteCond %{QUERY_STRING} p1\=([^&]+)&p2\=([^&]+)$
RewriteRule (.*) controller.php?parameter1=%1&parameter2=%2 [P,L,QSD]

The above works fine for me but I do notice it a tiny bit slow to process, so if anyone has any criticism then I would be glad to hear it!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.