DevHeads.net

webserver configuration to redirect correctly API calls

Hi,

I have to admit that I am a total amateur concerning apache and its configuration. However, I am in the need to do so. In order to use an API to add data into an application which is running in apache2 I need to set a redirection.

Everything what I found by now does not really help me, probably also as I do not know the syntax in detail and which files are really important.

I have the Info how this redirection works in Nginx:
location ~ ^/api/v1/(.*)/?$ {
rewrite /api/v1/(.*)$ /app/controllers/ApiController.php?req=$1? last;
}

In words what the redirection needs to do is that all requests that go to /api/v1/BLAH are redirected to /app/controllers/APIController.php?=req=BLAH

The "home" directory is: /var/www/html/elabftw
and the API stuff is in /var/www/html/elabftw/app/controllers/

I tried already something like that:
- creating a .htaccess file in /var/www/html/elabftw/
- adding:
RewriteEngine On
RewriteRule ^/api/v1/(.*)$ /app/controllers/ApiController.php?req=$1? [R=301]

..but it does not work...

I would be very happy and thankful for help - in which file (.htaccess??), do I need to write what...

Best,
Anke

Comments

Re: webserver configuration to redirect correctly

By Eric Covener at 03/13/2018 - 06:55

On Tue, Mar 13, 2018 at 3:51 AM, <a href="mailto:Anke. ... at gmx dot net">Anke. ... at gmx dot net</a>
<Anke. ... at gmx dot net> wrote:
With mod_rewrite, you need to watch out for a few gotchas (that you
unfortunatley walked right into)

1 - if you have access to the real conf, use it over htaccess. Rules
go in your <virtualhost>.
2 - rules have a slightly different syntax in htaccess -- the leading
prefix of the match is stripped off based on the htaccess location.
Nothing ever starts with /
3 - if you want to capture the query string, you need to add a
RewriteCond then use the %1 backreference.

Probably something close here:
<a href="https://wiki.apache.org/httpd/RewriteQueryString" title="https://wiki.apache.org/httpd/RewriteQueryString">https://wiki.apache.org/httpd/RewriteQueryString</a>

Re: webserver configuration to redirect correctly

By Anke.Wienecke at 03/13/2018 - 07:51

It is not my API. But I will forward your comment to the developper! Thanks!

Also thanks to Eric! I will go through "RewriteQueryString".

Re: webserver configuration to redirect correctly

By Frank Gingras at 03/13/2018 - 07:31

Why is your API not using pathinfo? You could simply use FallbackResource
/app/controllers/ApiController.php then.