user1797147
user1797147

Reputation: 927

Apache rewrite rule or solution, replace spaces with %20

We have a situation that need a brilliant idea. We have some embedded devices that sends GET requests with white spaces inside. That is, we cannot change that.

We discovered that when changing GSM operator, some devices failed to insert into DB and we discovered that this operator, doesn't alter our URL (like old one), so white spaces appears as the device sends them

GET /gsm/p.php?imei=865733029239522&operator=VODAFONE NL&v1=1&v2=0&v3=1&v4=0 HTTP/1.1" 200 568 "-" "QUECTEL_MODULE"

The problem became obviouse, Apache (2.4.18, linux) will stop parsing after first space that is VODAFONE and this is passed further as $_SERVER[]. All variables after are lost since we suspect that APACHE thinks that after first space will arrive type of protocol (HTTP/1.1") which is not true in our case.

Until fix of embedded, which is the best way to solve this? Can be a php problem and not Apache (we think not)?

ProtocolReqCheck - force Apache to behave like old days. RewriteRule - is this possible to replace ' ' with %20 on the fly.

UPDATE:

ProtocolReqCheck off

Like this suggestion isn't working any more, Apache2 is not even starting http://answers.google.com/answers/threadview/id/368359.html

I already tried this in .htaccess:

RewriteEngine on
RewriteRule (?:\s) %20 [N,DPI]

And verify by using this website http://htaccess.mwl.be/

But on this website is working partially, in sense of replacing all SPACES with %20 like expected but if a URL doesn't contain any SPACES will return nothing.

So basically I need to

But when I tested in my Apache, is not working at all, knowing because insertion in DB is wrong too. Also, checking Apache access.log, the URLs are not modified.

After rewrite rule, shouldn't be modified in access.log?

UPDATE2

Using @Dusan code, is working some how but enters in some rewrite loop

RewriteEngine On
RewriteCond %{THE_REQUEST} "^GET\s(.+)\s(.+)\sHTTP/1.1"
RewriteRule ^ "%1\%20%2" [PT]

The apache on windows is just not working right (logs are delayed) so once when worked, I was able to capture the rewrite rule

W:/wamp64/www/one/] RewriteCond: input='GET /one/entry.php?x=sdsfgsdfg dfgsdfgds222 HTTP/1.0' pattern='^GET\\s(.+)\\s(.+)\\sHTTP/1.0' => matched

[Mon Sep 05 19:22:40.573902 2016] [rewrite:trace2] [pid 6664:tid 884] mod_rewrite.c(476): [client 10.10.0.1:50322] 10.10.0.1 - - [localhost/sid#e65e50][rid#1d131e8/initial/redir#10] [perdir W:/wamp64/www/one/] rewrite 'entry.php' -> '/one/entry.php?x=sdsfgsdfg%20dfgsdfgds222'

So seems that URL is indeed transformed into

/one/entry.php?x=sdsfgsdfg%20dfgsdfgds222

But the actual page outputs 500 internal server error. Perhaps the rule is looping itself?

Upvotes: 2

Views: 4830

Answers (2)

user1797147
user1797147

Reputation: 927

Well, @Dusan Bajic thank you very much, appreciate! The solution found is as follows:

RewriteEngine On
RewriteCond %{THE_REQUEST} "^GET\s(.*)\s(.*)\sHTTP/1.0"
RewriteRule ^ "%1 %2" [END]

Basically this will fix the [QUERY_STRING] I still don't know how. Now it contains SPACES again, as embedded device send them out.

Initially I thought I'll need to convert them on fly to %20 which may be possible but with condition that further $_GET to contain correct values (stripped out) which is not (perhaps more complicated rewrite will do that too).

One more remark, you assumed that will be HTTP/1.1 but on broken by spaces, Apache will just take anything AFTER first space and interpret as protocol

[SERVER_PROTOCOL] => &var=wonder 123&vtype=OB10&rssi=23,0 HTTP/1.0
[SERVER_PROTOCOL] => HTTP/1.0   <-- this should be

So basically I replaced with SPACE too which is quite weird, replacing space by space but now, [QUERY_STRING] and all $_GET variables are intact and usable.

Finally, I found [END] to work, perhaps more elaborate here too.

Kind regards,

Upvotes: 1

Dusan Bajic
Dusan Bajic

Reputation: 10869

Can you try this:

RewriteEngine On
RewriteCond %{THE_REQUEST} "^GET\s(.+)\s(.+)\sHTTP/1.1"
RewriteRule ^ "%1\%20%2" [PT]

Tested on Apache 2.4.6.

Upvotes: 3

Related Questions