Apache “rewrite rule” for REST API

Here’s how you can configure your Apache webserver for a REST API, without using rewrite rules (thus: you don’t need the rewrite engine). Simply add the following line to your <VirtualHost>:

AliasMatch ^/api/rest/v1/(.*)$ /var/www/[mywebsite]/htdocs/api/rest/v1/index.php

In index.php, you can read the parameters like so:

   $path = preg_replace('~^/rest/v1/~', '', $_SERVER['SCRIPT_NAME']);
   $parts = explode('/', $path);

Leave a Reply

Your email address will not be published. Required fields are marked *