Giter Club home page Giter Club logo

php-apache-rewrite-proxy's Introduction

PHP Apache Rewrite Proxy

Purpose

If you need to develop the front-end part of a web application and have to make AJAX requests to an API which is not accessible through the same domain, these requests are blocked by the web browser due to same origin policy restriction.

To get around this you could send all API requests to the host you’re developing the front-end on while your local development host would only act as a proxy server for API requests, which means it accepts your AJAX request, sends the same request to the actual API host, and passes everything back to your JavaScript.

How does this script work?

The PHP script proxy.php does exactly what’s described above. It reads all request headers and the body, passes everything on to the remote host and returns all headers and body back to the client.

It also passes on the request method, which makes it compatible with GET, POST, PUT, DELETE or any other request methods you like.

There is also a sample .htaccess included which passes all requests starting with http://your-dev-host/api/ to proxy.php. If your API path starts with anything other than api/ simply change this.

To make things clearer, using the sample configuration a request to http://your-dev-host/api/endpoint?x=1 would be passed on to http://127.0.0.1:9000/api/endpoint?x=1.

Configuration options

  1. As described above, adjust the path of RewriteRule in .htaccess to ideally match the API path of the remote host.
  2. Set the correct base URL of your remote API host in $upstream_base_url in file proxy.php.
  3. You may set a $timeout in seconds for remote requests. Especially if your remote API host is not in your local network or responds slowly, you may want to increase the 5 seconds default value.

What does the proxy script change in requests and responses?

This script does not really add or remove anything to headers or body, generally it passes everything on in both ways as it is. But please be aware, your local server may add other headers like Server or X-Powered-By which were not in the API response. This rather simple implementation does also not take multiple headers with the same key into account. Headers are passed back to the client with the replace option of PHP’s header() function being true by default (see PHP header() documentation).

php-apache-rewrite-proxy's People

Contributors

rafasashi avatar manc avatar

Watchers

Jon "The Nice Guy" Spriggs avatar James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.