[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2041
  • Last Modified:

How can I avoid URL encoding from htaccess 301 redirect?

I'm using an htaccess page for 301 redirects, but the redirected URL is being sent to browsers with some URL encoding. I'm hoping to find a way to avoid the encoding.

The URL I'm redirecting to looks something like this:

http://www.site.com/index.php?iframe~iframe+url~http://www.site.com/plan.php?s=42&l=126

The line of code on my htaccess page looks like this:

RewriteRule ^this\.page$ http://www.site.com/index.php?iframe~iframe+url~http://www.site.com/plan.php?s=42&l=126 [R=301,L,NC]

The problem is that the 2nd '?' character is getting sent to the browser URL encoded, so the resulting URL looks like this:

http://www.site.com/index.php?iframe~iframe+url~http://www.site.com/plan.php%3fs=42&l=126

Is there a way to change the code on my htaccess page so that the URL will be sent exactly as is, without getting URL encoded?

Thanks.
0
Jonathan Greenberg
Asked:
Jonathan Greenberg
  • 4
  • 3
1 Solution
 
sjklein42Commented:
First try quoting it with a backslash and see if that has any effect:

RewriteRule ^this\.page$ http://www.site.com/index.php?iframe~iframe+url~http://www.site.com/plan.php\?s=42&l=126 [R=301,L,NC]

Open in new window

0
 
Jonathan GreenbergAuthor Commented:
No, no effect. Great idea, though.
0
 
sjklein42Commented:
Here's why you're getting burned.   Haven't found the workaround yet but looking.  Interesting.

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html

Down the page inside the RewriteRule section:

"One more note: You can even create URLs in the substitution string containing a query string part. Just use a question mark inside the substitution string to indicate that the following stuff should be re-injected into the QUERY_STRING. When you want to erase an existing query string, end the substitution string with just the question mark."
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
sjklein42Commented:
Here it is, I think:

•'noescape|NE' (no URI escaping of output)
This flag keeps mod_rewrite from applying the usual URI escaping rules to the result of a rewrite. Ordinarily, special characters (such as '%', '$', ';', and so on) will be escaped into their hexcode equivalents ('%25', '%24', and '%3B', respectively); this flag prevents this from being done. This allows percent symbols to appear in the output, as in
    RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE]
   
which would turn '/foo/zed' into a safe request for '/bar?arg=P1=zed'. Notice: The noescape flag is only available with Apache 1.3.20 and later versions.

RewriteRule ^this\.page$ http://www.site.com/index.php?iframe~iframe+url~http://www.site.com/plan.php?s=42&l=126 [R=301,L,NC,NE]

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
It's going to a PHP page, URLdecode it.  http://us.php.net/manual/en/function.urldecode.php  that page does say that "$_GET and $_REQUEST are already decoded. Using urldecode() on an element in $_GET or $_REQUEST could have unexpected and dangerous results. "

Does it work on that page?  Do a print_r ($_GET); to see if you really have a problem.
0
 
Jonathan GreenbergAuthor Commented:
Yeah, I think you've found what the problem is. I hope there's a solution.
0
 
Jonathan GreenbergAuthor Commented:
Sorry, my last comment was meant for post at ID 34985457. Please ignore it.
0
 
Jonathan GreenbergAuthor Commented:
sjklein42: thanks, that does the trick! Exactly what I was hoping for!

DaveBaldwin, thank you, too, for your input.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now