0

I write website prototypes that use data from a server I do not control, the response is in JSON and the server does not support JSONP, thus I have problems with CORS as I am developing in the client only.

I have managed to get around this by running a local apache HTTP webserver with the following ProxyPass rules in my vhosts.conf

ProxyPass /api/de/ http://de.test.com/api/
ProxyPassReverse /api/de/ http://de.test.com/api/
ProxyPass /api/jp/ http://jp.test.com/api/
ProxyPassReverse /api/jp/ http://jp.test.com/api/
ProxyPass /api/ru/ http://ru.test.com/api/
ProxyPassReverse /api/ru/ http://ru.test.com/api/
ProxyPass /api/uk/ http://uk.test.com/api/
ProxyPassReverse /api/uk/ http://uk.test.com/api/

As you can see there are multiple subdomains for the same domain, each one is for accessing a different locale, in real terms this means I get the data in a different language, which is important for my prototypes.

There are more than 30 languages that I would like the ability to test in my prototypes and I want to know if there is a way to write my ProxyPass rules dynamically (or a better solution).

I'm using jQuery with the getJSON method and an example of the URL I am hitting is as follows

url = '/api/'+pos+'/data.html?destination=...

Where pos is simply a string I pass through with 'de', 'jp', 'ru', or 'uk'

Blaise
  • 3
  • 1

1 Answers1

0

You can achieve it with mod_rewrite

RewriteEngine On
RewriteRule /api/([a-z]+)/(.*) http://$1.test.com/api/$2 [P]

The [P] on mod_rewrite makes it act as mod_proxy (from apache doc http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_p):

Use of the [P] flag causes the request to be handled by mod_proxy, and handled via a proxy request. For example, if you wanted all image requests to be handled by a back-end image server, you might do something like the following:

RewriteRule /(.*).(jpg|gif|png) http://images.example.com/$1.$2 [P]

alphamikevictor
  • 1,062
  • 6
  • 19