URLs being decoded and then re-written

Oh, the strange things mod_rewrite does!

URLs being decoded and then re-written

Postby fizi » Thu Jun 11, 2009 3:14 pm

We're currently using a simple rewrite rule to automatically rewrite http URLs to https URLs.

Current rule:

Code: Select all
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.ca$1 [R=permanent,L,NE]


This has worked just fine for us up until now. It seems we didn't count on the handling of characters like question marks and how this rule would affect those.

Take this URL for example:

Code: Select all
http://example.ca/some/folder/path/word%3F/


After it goes through our mod_rewrite rule we get:

Code: Select all
https://example.ca/some/folder/path/word?/


This of course breaks our web application which is expecting %3F and not a decoded ?. To top it off the application is third party and we can't just go alter the source code.

After some research I found a debate in Apache's Bugzilla (https://issues.apache.org/bugzilla/show ... i?id=34602) about whether or not this was a bug or by design. I tried using the 'B' flag they recommend around post 27 but it appears the version of Apache in RHEL 4 and 5 does not include the patch that added the 'B' flag.

So how can I fix this bug or no bug?
fizi
 
Posts: 3
Joined: Thu Jun 11, 2009 2:59 pm

Postby richardk » Fri Jun 12, 2009 8:36 am

Try it without the NE (No Escape) flag.

Also try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteCond %{THE_REQUEST} \ (/[^\ ]*)\  [NC]
RewriteRule . https://example.ca%1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby fizi » Fri Jun 12, 2009 8:43 am

I had tried removing the NE before with no luck. I re-checked it and I get the same results.

I tried your mod_rewrite rule and it's oh so close.

The browser page returned says:

Code: Select all
Not Found
The requested URL /some/folder/path/word%3F/ was not found on this server.


Which looks perfectly fine to me but the address bar in the browser shows:

Code: Select all
https://example.ca/some/folder/path/word%253F/


Mod re-write wants to convert the % sign now. I'm not sure if this will bugger our application or not. I'll have to try it and see. I'll let you know what I find.
fizi
 
Posts: 3
Joined: Thu Jun 11, 2009 2:59 pm

Postby richardk » Fri Jun 12, 2009 8:50 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SERVER_PORT} 80
RewriteCond %{THE_REQUEST} \ (/[^\ ]*)\  [NC]
RewriteRule . https://example.ca%1 [NE,R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby fizi » Fri Jun 12, 2009 10:30 am

Bingo! That looks like it's done it.

URL shows:

Code: Select all
http://example.ca/some/folder/path/word%3F/


Browser page shows :

Code: Select all
Not Found
The requested URL /some/folder/path/word?/ was not found on this server.


I have a feeling that's just Firefox rendering the HTML. I'll try this rule and see how things go.
fizi
 
Posts: 3
Joined: Thu Jun 11, 2009 2:59 pm


Return to Idiosyncrasies

Who is online

Users browsing this forum: No registered users and 0 guests