phpapache

Configure Apache2 to host REST API


Since I want to learn more around hosting a REST API and also development of such (with PHP) I am wondering about the following: I've got my API files in the path /var/myapi and configured the directory like this:

<Directory /var/myapi>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

The Virtual Host is configured like this:

<VirtualHost *:8080>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/myapi/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

In my api directory I want to have the index.php is my router. So, the $_SERVER["REQUEST_URI"] should contain the full path the client is requesting.

Unfortunately, when accessing http://localhost:8080/books the path /books will be handled by apache which is trying to find a file in /var/msapi/books. The path ``/books` will not be reflected within my routers PHP code.

I think I somehow need to change settings in Apache but do not know what to look for. Any advice?


Solution

  • To route all requests through index.php and ensure that the $_SERVER["REQUEST_URI"] contains the full path the client is requesting, you need to modify your Apache configuration to use URL rewriting. This can be done with the mod_rewrite module, which allows you to rewrite URLs dynamically.

    Here’s how you can set it up:

    1. Enable mod_rewrite Module

    First, ensure that the mod_rewrite module is enabled in Apache:

    sudo a2enmod rewrite
    sudo systemctl restart apache2
    
    1. Update the Apache Virtual Host Configuration

    You need to add a RewriteRule to redirect all requests to index.php, except for existing files and directories.

    <VirtualHost *:8080>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/myapi/
    
        <Directory /var/myapi>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
    
            # Enable URL Rewriting
            RewriteEngine On
    
            # Check if the request is for a file or directory that exists
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
    
            # If not, redirect all requests to index.php
            RewriteRule ^ index.php [L,QSA]
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    

    Here’s how your updated VirtualHost configuration should look:

    Creating an .htaccess file (Optional)

    Alternatively, you can use an .htaccess file within the /var/myapi directory if you don’t want to modify the VirtualHost configuration directly. Make sure the AllowOverride directive is set to All in your VirtualHost config to allow .htaccess to override configurations.

    Create an .htaccess file in /var/myapi:

    RewriteEngine On
    
    # Check if the request is for a file or directory that exists
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # If not, redirect all requests to index.php
    RewriteRule ^ index.php [L,QSA]
    

    After making the changes, restart Apache to apply the configuration:

    sudo systemctl restart apache2