3

I am currently trying to use mod_proxy to make search requests on another server. The remote server requests must be structured as follows:

http://path.to.remote/search.php?key=MYKEY&term=SEARCHTERM

In an effort to obfuscate my access key, I was hoping to contain the authentication within my vhost definitions. Since the key must be passed as a query string value, I thought it would be as simple as:

ProxyPass /lookup/t/ http://path.to.remote/search.php?key=MYKEY&term=

Where my URLs would look like

http://localhost/lookup/t/term=SEARCHTERM

I quickly discovered that ProxyPass preforms mandatory character escapes on the target URL which rendered my URL unusable. I found lots of people who had similar issues, primarily with previously encoded characters, and that URL encoding with ProxyPass was unavoidable.

I found a few suggestions that I should use mod_rewrite to assemble the proxy request for me, but I wasn't sure how to do so. I ended up with rules in my vhost definition that look something like:

RewriteEngine On
RewriteCond     %{REQUEST_URI}      !^/lookup/t/
RewriteRule ^/lookup/t/$ http://path.to.remote/search.php?key=MYKEY&term=$1

I don't have a lot of experience with mod_rewrite, and my regex skills aren't great, so I'm hoping someone can explain how I would rewrite my URL and how to funnel it through mod_proxy as needed. Am I correct in thinking I need to use rewrite to assemble the query string like so:

Http://localhost/lookup/t/term=SEARCHTERM  
Http://localhost/lookup/t/?key=MYKEY&term=SEARCHTERM  

and then use mod proxy to go from

/lookup/t/   

to

http://path.to.remote/search.php
user1026361
  • 173
  • 1
  • 1
  • 7

1 Answers1

2

My suspicions turned out to be correct. I had to modify the query string with mod_rewrite and then proxy it to my destination. In the end:

  1. I enabled mod_rewrite and mod_ssl
  2. I added an SSLProxyEngine directive to my vhost definition:
        SSLProxyEngine On
  3. I turned on rewrite and added a rule in my vhost def. I also turned on logging for debugging. I learned that I wouldn't be able to rewrite a request for the directory so I had to point to a script. I also had to use the QSA and P options:
        RewriteEngine On
        RewriteRule ^/lookup.php /lookup?key=MYKEY [QSA,P]

  4. I set up my proxy rules. Since the query string is constructed, simple proxying rules apply:
        ProxyPass /lookup https://path.to.remote/search.php
        ProxyPassReverse /lookup https://path.to.remote/search.php

  5. Don't forget to restart apache! (I did)

  6. I now connect to http://localhost/lookup.php?term=SEACRHTERM
user1026361
  • 173
  • 1
  • 1
  • 7