Redirect Rest service with .htaccess

0

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

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

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

The file .htaccess of the app/controllers/rest/ folder 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();
When doing a request for url app/controllers/rest/foo , it returns the result, however if request is done using the url api/foo , the framework does not can identify the /foo parameter.

Note: I am working in the Linux / ubuntu environment

    
asked by anonymous 17.03.2015 / 04:13

1 answer

1

After several trial-and-error tests, I discovered that targeting with .htaccess must be done with the full address. First I tried to redirect with code HTTP 301 ( [R=301,L] ), but I noticed that when directing the page it treated as GET all types of request ( POST , PUT , DELETE ). Searching the internet I found that by changing the code HTTP to 300 ( [R=300,L] ), redirection works perfectly for any method. The file .htaccess of the api folder looks 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 file .htaccess of the app/controllers/rest/ folder did not have to be changed.

    
20.03.2015 / 01:05