Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


restful api / endpoints / .httaccess

restful api / endpoints / .httaccess

restful api / endpoints / .httaccess

After so many years of writing mostly procedural PHP code, I am at a point where I must step out of my comfort zone and write a RESFUL API.

Not a big deal, really, except that for the life of me, the endpoints are kicking my rear-end.

My problem:

if I define my URI to be api.sitename.com which is just a vhost that points to sitename.com/api

within ~/api directory I created .htaccess file that looks as follows


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ index.php?request=$1 [QSA,NC,L]

my index.php script I look for endpoints by exploding the URI and the returned elements are validated to
1. identify class
2. identify method

so, given this scenario, the URI api.sitename.com/rfq/get should call $rfq->get() but instead I am getting error 404

The above .htaccess file is in ~/api directory or root directory for api.sitename.com. Do I need to create subdirectories for every possible endpoint and place the .htaccess file in each of these endpoints? If that is the case, this is crazy ...

I am frustrated, this should not be so confusing or I should not be so clueless.

Thank you all for you assistance!

The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: restful api / endpoints / .httaccess

Solved it!

I found the answer here:


In my case, I did the following (I run Ubuntu 16.x.x)
1. edited /etc/apache2/apache2.conf - I added


<Directory /var/www/html/api>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride All
  Order allow,deny
  allow from all

2. In /var/www/html/api/.htaccess, I added


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
RewriteRule ^.*$ /index.php [L,QSA]
FallbackResource /index.php

ErrorDocument 404 /api404.php 
Notice that api404.php is simply my own custom 404 page to present visitor with a custom/controlled message in lieu of standard out.

3. I then, as noted in the above link, enabled module_override by running the following commands
(a) sudo a2enmod rewrite
(b) sudo service apache2 restart

After doing above steps, my URI https://api.sitename.com/rfq/new redirected to my index.php script and I was able to handle parsing of URI as intended.

Hope this helps others remedy their problem, I sure was frustrated with this but close to 24 hours later, finally solved it!

The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close