Duncan Palmer Duncan Palmer - 14 days ago 8
Apache Configuration Question

htaccess re-write for restful api

I am creating a restful based API and am stuck on htaccess re-write rules.

I am currently using this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !(.*)\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico)$ [NC]
RewriteRule ^(.*)$ index.php?handler=$1 [QSA,L]
</IfModule>


Which works, however I want it to exclude file extension directories and the current exclusion doesn't make a difference.

E.g.


  • /api/object/1/list is valid

  • /api/object/test.php/js/css etc.. is not valid



Thanks.

Answer

Your RewriteCond includes a . before all the included extensions.

Remove the \. from your condition and it should work like so.

RewriteCond %{REQUEST_URI} !(.*)(css|js|htc|pdf|jpg|jpeg|gif|png|ico)$ [NC]

Do note that you will not be able to use the included extension names in your method/object name or method/object name ending with these extension names.