Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite query

         

spike2000

3:39 pm on Oct 13, 2008 (gmt 0)

10+ Year Member



In our application we need to interrogate the headers of a request for a value "msisdn". Depending on the wap gateway that forwards this query, the query will be included in the header in one of three ways.

1) Via the x-up-calling-line-id= value at the end of the header
2) Via the x-msisdn= value at the end of the header
3) Via a value msisdn included in the query (e.g. /page.jsp?msisdn=

Is it possible to create a mod_rewrite rule that will standardise this value to for example msisdn= in the header and if so, could you provide some guidance on how to do this.

I came across someone who had a similar issue and they solved it as follows;

# Request headers
<IfModule !mod_headers.c>
LoadModule headers_module modules/mod_headers.so
</IfModule>

# Copy the MSISDN to an internal variable
RewriteEngine On
RewriteCond %{HTTP:MSISDN} .*\s(.*)
RewriteRule .* - [E=MSISDN:%1]

# Set the XX-MSISDN header
RequestHeader set XX-MSISDN %{MSISDN}e

However when I tried this, it broke the query completely.

jdMorgan

5:10 pm on Oct 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It'd probably be much easier to help you if you'd show the typical contents of the header, clarify the exact name of the header (you show three variations), and provide multiple examples of typical calling URLs (use example.com for the domain, please) and the associated request headers.

The code above uses two modules when one would do, and the naming of the variable is inconsistent -- x-msisdn, MSISDN, and xx-MSISDN are all mentioned.

Also, what is the specific, detailed meaning of "broke the query completely"?

With server config code, everything is in the details...

Jim

spike2000

6:41 pm on Oct 13, 2008 (gmt 0)

10+ Year Member



Hi, thanks for your prompt reply. Here are examples of the three different implementations of the msisdn header that we see

GET /m/page.jsp;jsessionid=7CE68B4099B3AE8BF3D4BF1E767FDA52.worker3 HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.example.com
Cookie: JSESSIONID=7CE68B4099B3AE8BF3D4BF1E767FDA52.worker3
x-msisdn: 27821111111

GET /m/page.jsp;jsessionid=7CE68B4099B3AE8BF3D4BF1E767FDA52.worker3 HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.example.com
Cookie: JSESSIONID=7CE68B4099B3AE8BF3D4BF1E767FDA52.worker3
x-up-calling-line-id: 27831111111

GET /m/page.jsp?msisdn=27841111111;jsessionid=7CE68B4099B3AE8BF3D4BF1E767FDA52.worker3 HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.example.com
Cookie: JSESSIONID=7CE68B4099B3AE8BF3D4BF1E767FDA52.worker3

Note that in the last example the network appends the msisdn field to the actual query.
I'm not sure how it actually broke the query but I could not aaccess our site.

jdMorgan

10:45 pm on Oct 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, well it's till not clear how the header info is passed to the script, but here's one way to get the x-msisdn request header value, the x-up-calling-line-id request header value, or the msisdn query string value into the server variable called MSISDN, and then create a new request header called XX-MSISDN containing that value plus the letter "e", which seems to be the function of your existing code.

This code implements a priority scheme such that the variables are accepted in this priority: if present in the HTTP request, x-msisdn overrides x-up-calling-line-id, which (if present) overrides the query string "msisdn=xyz" name/value pair. If none are present, then the MSISDN variable will not be set, and the value of the XX-MSISDN request header will be the single character "e".

This code is intended only for use in a server config file (e.g. httpd.conf or conf.d) and not for use in .htaccess files.


# Enable mod_rewrite
Options +FollowSymLinks
RewriteEngine on
#
# Get X-MSISDN, X-UP-CALLING-LINE-ID, or msisdn query string value to server variable MSISDN
RewriteCond %{HTTP:X-MSISDN} ^((.+))$ [OR]
RewriteCond %{HTTP:X-MSISDN}>%{HTTP:X-UP-CALLING-LINE-ID} ^>((.+))$ [OR]
RewriteCond %{HTTP:X-MSISDN}>%{HTTP:X-UP-CALLING-LINE-ID}>%{QUERY_STRING} ^>>([^&;]+[&;])*msisdn=([^&;]+) [NC]
RewriteRule ^/m/page\.jsp$ - [E=MSISDN:%2]
#
# Load mod_headers module if not already compiled-in
<IfModule !mod_headers.c>
LoadModule headers_module modules/mod_headers.so
</IfModule>
#
# Set request header XX-MSISDN to value of MSISDN variable, plus "e"
RequestHeader set XX-MSISDN %{MSISDN}e

I should note that the character ">" which appears several time above is meaningless; It is used only the delimit the variable values so that they can be parsed by the pattern-matching engine. Also, the extra parentheses are used only so that no matter which RewriteCond might be true, the resulting value ends up in local variable %2 (to resolve local variable numbering, count left parentheses).

As shown, the rule is only applied to requests for the /m/page.jsp URL-path.

If this does not work, then I'm afraid you're going to have to find someone at your end who knows how this whole thing is supposed to work -- Exactly how the header info is supposed to be prioritized and passed, and what/where and when that info is supposed to be passed to. I'll freely admit that I'm just guessing here.

For example, is the MSISDN variable used anywhere but in this code? How about the msisdn query string name/value pair? And finally, why is it necessary to have and use three different methods of passing this data? -- It all seems a bit over-complicated to me.

Jim

spike2000

9:55 am on Oct 14, 2008 (gmt 0)

10+ Year Member



Hi Jim

Thank you for your assistance with this, it's much appreciated. I think I just need to clarify one or two things.

The code I posted which showed a Rewrite Rule using XX-MSISDN was not my own but something I came across on the internet, hence their requirement was slightly different from mine.

The reason there are three different methods of passing the data has to do with the three cellular networks who forward requests from their customers via a wap gateway, each of which implements the msisdn value differently.

I tried implementing your recommendation above, with one change. I modified

RewriteRule ^/m/page\.jsp$ - [E=MSISDN:%2]

to

RewriteRule ^/$ - [E=MSISDN:%2]

The reason for the change is that I need the rule applied for all URL requests and not just for that page or directory.

Using the Live HTTP headers plugin on Firefox, the change did not seem to make any difference. I also tried to enable logging of re-writes by adding the following in my Virtual Host entry below the code you gave me.

RewriteLog "/usr/local/apache/logs/rewrite.log"
RewriteLogLevel 3

No entries were created in the file and so I moved the two lines above to my httpd.conf and tried again with no result.

Any idea what I could be doing wrong?

jdMorgan

6:48 pm on Oct 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The regular-expressions pattern "^/$" means "exactly a slash." Therefore, the rule would only be applied to the directory index page in root. I'd suggest "^/", ".*", or ".?" which are functionally equivalent at the server config level.

See the short regular expressions tutorial in our Forum Charter for more info.

You're going to need to find out how your scripts expect the msisdn value to be passed to them, and tweak the code to suit.

Jim

spike2000

6:04 am on Oct 17, 2008 (gmt 0)

10+ Year Member



Hi, it's still not working. I got an error message when I tried to re-start Apache with the new config. Unfortunately I forgot to note it down and since this is a production box, I need to find an appropriate time to try it again.