Redirect Rest service with . htaccess

Asked

Viewed 635 times

0

I have a folder in my project called api, and within it a file .htaccess that should redirect the requests (made via jquery) to the folder app/controllers/rest/ which also has a file .htaccess and a file called endpoint.php, where the requests are treated and where interaction with the framework Slim.

The . htaccess folder file api, has the following parameters:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ ../app/controllers/rest/$1 [QSA]

The archive .htaccess of the briefcase app/controllers/rest/, has the following parameters:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ endpoint.php [QSA,L]

And the file endpoint.php has the following code:

$app = new \app\libs\Slim\Slim();
$app->get('/foo', function () {
   echo '{"teste": "TESTE"}';
});
$app->run();

By making a request to url app/controllers/rest/foo, it returns the result, however if the request is done using the url api/foo, the framework cannot identify the parameter /foo.

Note: I am working on Linux/Ubuntu environment

1 answer

1


After several trial and error tests I found that directing by .htaccess must be done with the full address. I first tried to redirect with the code HTTP 301 ([R=301,L]), but I realized that when I directed the page the same treated as GET all kinds of request (POST, PUT, DELETE). Searching the internet I found that changing the code HTTP for 300 ([R=300,L]), redirecting works perfectly for any method. The file .htaccess of the briefcase api was like this:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ http://%{HTTP_HOST}/app/Controllers/rest/$1 [R=300,L]

The archive .htaccess of the briefcaseapp/controllers/rest/, did not need to be changed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.