I'm making a little REST API in PHP but I'm trying to rewrite URL with .htaccess
I'm trying to rewrite URL like this
localhost/api/object_attributes/1/1
to
localhost/api/object/1/attributes/1
In my .htaccess I already made a condition to remove .php
but the second one is not working.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^api/ api.php
RewriteRule ^api/object/1/attributes/1 /object_attributes/1/1
</IfModule>
I searched on the web but didn't find any solution and I get no errors / redirections.
That second rewrite rule you are working on does not really make sense in my eyes. There is no object to be found under that path in your http server.
I assume you are looking for one of the following two approaches. Which one is not getting clear from your description, the term "I want to rewrite from ... to ..." is ambiguous.
RewriteEngine On
RewriteRule ^/?api/object/(\d+)/attributes/(\d+) /api.php?object=$1&attributes=$2 [END,QSA]
RewriteEngine On
RewriteRule ^/?api/object_attributes/(\d+)/(\d+) /api.php?object=$1&attributes=$2 [END,QSA]
A general hint: you should always prefer to place such rules inside the real http servers host configuration instead of using .htaccess
style files. Those files are notoriously error prone, hard to debug and they really slow down the server, often for nothing. They are only provided for situations where you do not have control over the host configuration (read: really cheap hosting providers) or for applications that need to write their own rewrite rules in a primitive way (which is an obvious security nightmare).