Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

restful api / endpoints / .httaccess 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
0
0
US
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
Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ index.php?request=$1 [QSA,NC,L]
</IfModule>

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!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
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
Code:
<Directory /var/[URL unfurl="true"]www/html/api>[/URL]
  Options Indexes FollowSymLinks MultiViews
  AllowOverride All
  Order allow,deny
  allow from all
</Directory>

2. In /var/ I added
Code:
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 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!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top