Setting up a PHP REST API on App Platform without .php extension

Posted March 15, 2021 976 views
APIDigitalOcean App Platform

I am trying to set up a PHP REST API on App Platform.
The first issue I noticed is that I can’t set a URL without the .php extension. Not sure if there’s any workaround for this at the moment.

I noticed that we have catchall_document for static sites for some URL routing; but we don’t have that for serverside apps.

Seems like the only “solution” is to open a new folder for each path / file with a index.php file which is not really a good solution.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Hi, I am not sure what you mean exactly, PHP app will not work as static site at all as it will serve only your source code, no PHP interpreter there, but if you working with Dynamic Web APP then using .htaccess you can achieve what you want just like in any other environment.

  • Hi, app platform does not only support static sites, but server-side apps as well. I am not building a static site. I’m just saying that static sites have similar solutions now but not server-side apps.

    • Hi @mudora23, .htaccess files are supported like @aolbrich suggested. Add it to your document root and commit it to your git repo and it should take effect. Something like this might work to hide .php extensions:

      # Run Php without filename extension
      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME}.php -f
      RewriteRule ^(.*)$ $1.php
      # Return 404 if original request is .php
      RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
      RewriteRule .* - [L,R=404]

      Source: how to hide php extension in url using .htaccess

      Alternatively, if you want to route all requests to index.php, you can use this which is what the Laravel framework uses:

          RewriteEngine On
          # Handle Authorization Header
          RewriteCond %{HTTP:Authorization} .
          RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
          # Redirect Trailing Slashes If Not A Folder...
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteCond %{REQUEST_URI} (.+)/$
          RewriteRule ^ %1 [L,R=301]
          # Send Requests To Front Controller...
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteRule ^ index.php [L]

      Source: laravel/laravel public/.htaccess