Search code examples
apachefastcgiphp

php-fpm - How to execute certain symlinks as PHP scripts


I am running Apache 2.2 with FastCGI and php-fpm. I am trying to duplicate the following logic:

<FilesMatch "^(admin|api|app)?(_dev)?$">
    #ForceType application/x-httpd-php
    SetHandler php-fcgi
</FilesMatch>

Which allows me to symlink admin.php as admin, so I can remove the .php extension. It seems the only way to do this with php-fpm is to set the security.limit_extension of the www.conf file to empty, however, as the comments indicate, this is a pretty big security hole, in that php code can now be executed from within any file, regardless of extension.

What would be the preferred way to accomplish the above, but still maintain some semblance of security?


Solution

  • @Mike, based on your updated answer, something similar to this .htaccess file should be able to handle what you're trying to do:

    # Enable the rewrite engine
    RewriteEngine on
    # Set the rewrite base path (i.e. if this .htaccess will be running at root context "/" or a subdir "/path")
    RewriteBase /
    
    # If the file exists, process as usual.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .* - [NC,L]
    
    # If the dir exists, process as usual (if you don't need this, just comment/remove the next two lines).
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule .* - [NC,L]
    
    # If (requested_file_name).html exists, rewrite to that file instead.
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.*)$ $1.html [QSA,L]
    
    # If (requested file name).php exists, rewrite to that file instead.
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.html [QSA,L]
    
    # If none of the above rules were triggered, fallback to index.php.
    RewriteRule ^(.*)$ index.php [QSA,L]
    

    With a bit of tweaking this should be able to do the job without the need of having to dive into httpd.conf and the <VirtualHost> nor <FilesMatch> directives. Hope this helps.