mod_rewrite RewriteRule pattern match fails when query string is URL-encoded - mod-rewrite

I am trying to use mod_rewrite RewriteRule, and in my RewriteRule, I am trying to match a URL that has query string that looks like:
http:///myfakeoam/obrareq.cgi?....
My RewriteRule looks like:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^wh=(.*)$ [NC]
RewriteRule ^/myfakeoam/obrareq.cgi$ http://apache1.whatever.com/formbasicprotected/index.html [CO=wh:%1:.whatever.com:1440:/]
When I test manually, by manually typing the URL into the browser, that RewriteRule seems to be able match a request if the request looks like:
http:///myfakeoam/obrareq.cgi?wh=xxx&ru=yyyy&....
but, in my actual system, the request is being created by anoher app, and it appears to be URL-encoding (actually URL-encoding twice) the query string (e.g, replacing equal with "%3D", etc.), i.e.:
http:///myfakeoam/obrareq.cgi?wh%3Dxxx....
And if the query string part is URL-encoded like that the pattern match in my RewriteRule is not failing...
Is there any way to handle this situation?
Thanks,
Jim

Answering my own question, I just realized that the RewriteCond was just doing a regex match for the query string, so I changed that to:
RewriteCond %{QUERY_STRING} ^wh%3D(.*)$ [NC]
and then it worked.
Jim

Related

Rewrite rule on URL query string

Can somebody help me how to replace query string URL. I want to redirect URL (my requirement to remove %2Fcontent%2Fabc from query string URL).
From :
http://something.com/en-US/login.html?resource=%2Fcontent%2Fabc%2Fen-US%2Fxyz.html&$$login$$=%24%24login%24%24
to:
http://something.com/en-US/login.html?resource=%2Fen-US%2Fxyz.html&$$login$$=%24%24login%24%24
I tried using rewrite condition and rule below but no luck.
RewriteCond %{QUERY_STRING} ^resource=%2Fcontent%2Fabc(.*)$
RewriteRule ^/(.*)?resource=%2Fcontent%2Fabc(.*)$ /$1?resource=%2 [L]

ISAPI_REWRITE a simple search and replace?

Using ISAPI REWRITE v3
I need to replace a string in an URL. The old string is 'p=type1_' and the new is 'p=type2_'. So
http://www.somesite.com/cgi-bin/script.pl?t=something&p=type1_abcde
becomes
http://www.somesite.com/cgi-bin/script.pl?t=something&p=type2_abcde
I reckon this should work:
RewriteRule ^(.*)p=type1_(.*)$ $1p=type2_$2 [NC]
But I get a 'Pattern Not Matched' in the ISAPI_REWRITE RegexTest app as soon as the original string contains a '?' - which, in practice, it always would.
How do I do this simple search and replace?
You will need to use RewriteCond %{QUERY_STRING} to search the querystring.
Try something like this:
RewriteCond %{QUERY_STRING} t=(.*)&p=type1_(.*)
RewriteRule ^cgi-bin/script.pl$ cgi-bin/script.pl?t=$1&p=type2_$2 [NC]

Rewrite URL so that query string is in path

I'm trying to get the following path: /faculty/index.php?PID=FirstLast&type=alpha
To rewrite to this: /faculty/FirstLast
Am I correct to assume the following would be acceptable to put in .htaccess?
# Rewrite old URLS
RewriteCond %{QUERY_STRING} ^PID=([0-9a-zA-Z]*)$
RewriteRule ^/faculty/index.php$ /faculty/%1 [R=302,L]
I'm okay to throw away any other query string variables. I'm applying these rules at the .htaccess file level. This project is a migration from an older system into Drupal.
Outcome:
My .htaccess looks like
# Rewrite old URLS
RewriteCond %{QUERY_STRING} PID=([0-9a-zA-Z]*)
RewriteRule ^faculty/ /faculty/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} vidID=([0-9]*)
RewriteRule ^videos/ /video/id/%1/? [R=301,L]
I also found this wonderful tool -- a mod_rewrite tester
http://htaccess.madewithlove.be/
All good!
Try this instead:
RewriteRule ^faculty/index.php$ /faculty/%1? [R=302,L]
The leading slash is not in the URI-path tested in the rule, so can't be in the regex either.
As the query is automatically appended to the substitution URL (passed through unchanged) unless a new query is created in the rule, the trailing question mark ? erases the existing query string when the rule is used.

How can I turn off QSA? (query string append)

I'm using Apache2 and mod_rewrite to hide my query strings. These are the rules in question.
RewriteCond %{QUERY_STRING} ^query=(.*)$
RewriteRule (.*) /search/%1 [R=301,L]
RewriteRule ^search\/?$ /search/?query=test [R=301,L]
When I visit /search (or /search/) I am correctly redirected to /search/?query=test (as per the last rule)
From there, the RewriteCond and RewriteRule should kick in and redirect me to /search/test, right? From what I understand the %1 in my first RewriteRule corresponds to the (.*) in the RewriteCond which should contain test.
However, what actually happens is I'm redirected to /search/test/?query=test. So, the rule works, but for some reason the query string appended. Is it the QSA option being automatically added somehow/somewhere?
I'm then stuck in an infinite loop of redirecting to /search/test?query=test because the first RewriteCond and RewriteRule kick in again, and again, and again...
What am I doing wrong?!
Thanks!
You need to specify an empty query in the substitution to prevent the original requested query to be appended to the new URL:
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.
So:
RewriteCond %{QUERY_STRING} ^query=(.*)$
RewriteRule (.*) /search/%1? [R=301,L]

How to forward one url to another using mod_rewrite rules

Im trying to forward this url, but its not working - here is my code:
RewriteRule ^https://www.wsjwine.com/discovery_offer.aspx?promo=2227006$ https://www.wsjwine.com/discovery_offer_lp2.aspx?promo=2227006 [L]
With RewriteRule directive you can only test the URL path. For further tests you need to use additional RewriteCond directives.
Now if you want to rewrite every request of /discovery_offer.aspx to /discovery_offer_lp2.aspx regardless of how the query looks like, you can just use this (example for the .htaccess file in the root directory):
RewriteRule ^discovery_offer\.aspx$ discovery_offer_lp2.aspx [L]
If you don’t specify a query in the substitution, the originally requested query is automatically appended to the new one.
And if you just want to rewrite that specific URL, try this:
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} =www.wsjwine.com
RewriteCond %{QUERY_STRING} =promo=2227006
RewriteRule ^discovery_offer\.aspx$ discovery_offer_lp2.aspx [L]
You can't detect query strings like that. Use RewriteCond %{QUERY_STRING}.

Resources