The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

.htaccess configuration for Slim PHP REST API on a subdomain?

Dylan Staley's Avatar

Dylan Staley

30 Jun, 2012 01:28 AM

I currently have an api.php file built using the Slim Framework. I'd like to use something like api.mydomain.com (or mydomain.com/api) to reference the API file and make calls using http://api.mydomain.com as the base url. The Slim documentation calls for the following to be added to your .htacess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

But if I'm correct, that returns the index.php file if the file requested doesn't exist. How best to setup a rewrite from api.mydomain.com or mydomain.com/api to the index.php? Everything we've found points to configuring this using .htaccess.

We used the following rewrite rule, but this resulted in having slashes ('/') appended to all of our URLs, which also required us to reconfigure our routes in Slim as well. (Example: "/messages" became "/messages/" and "/threads?id=123" became "/threads/?id=123")

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^api\.mydomain\.com [NC]
RewriteRule ^ api.php [QSA,L]
  1. Support Staff 1 Posted by Brian Nesbitt on 30 Jun, 2012 12:51 PM

    Brian Nesbitt's Avatar

    You could just use apache name based virtual hosts and just setup api.mydomain.com as a named virtual host. Then it will behave like the only site on the domain and there is no need to check the HTTP_HOST in the .htaccess since the named based virtual host only responds to the configured ServerName... in your case api.mydomain.com.

  2. 2 Posted by csuser on 02 Jul, 2012 08:54 PM

    csuser's Avatar

    Thanks - worked for us.

  3. Brian Nesbitt closed this discussion on 03 Jul, 2012 02:36 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac