using .htaccess for REST does 301 redirect
-
22-08-2019 - |
Question
I'm trying to build an extremely simple RESTful test rig for an app I'm working on. I'm using .htaccess to redirect urls to my controller script where I will parse the urls to handle the logic.
I have the redirect working, but the problem is that if I do a POST/PUT/DELETE, Firebug shows my requests going to the test rig properly, but I get a 301 redirect response and then a 2nd GET response.
How can I use htaccess for my url redirection without it sending a 301 to the browser?
This is the .htaccess I'm using now:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
Solution
Try something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
This will redirect unless the file asked for exists(such as an image or CSS file). I frequently use these rules and I've never have run into a 301.
Check that your PHP script isn't changing the response to a 301.