apachenginxserverconfigurationvhosts

What is the Apache equivalent of nginx's try_files directive


Please help, i'm new to server configuration.

I'm trying to write the equivalent of the nginx server configuration below in Apache's virtual host configuration.

server {

    root /path/to/public/root/directory;
    server_name myservername.test;

    location /api {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location /web {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location / {
        proxy_pass http://localhost:80;
    }

}

From what i have researched, I tried to do the following

<VirtualHost *:80>
        ServerName myservername.test
        DocumentRoot "/path/to/public/root/directory"

        <Directory /path/to/public/root/directory/>
                RewriteEngine on
                RewriteCond %{REQUEST_FILENAME} !-d  
                RewriteCond %{REQUEST_FILENAME} !-f

                RewriteCond %{REQUEST_FILENAME} "=/web" [OR]    
                RewriteCond %{REQUEST_FILENAME} "=/api"

                RewriteRule . index.php [QSA, L]
        </Directory>    
</VirtualHost>


<VirtualHost *:80>
        ServerName myservername.test
        ProxyPreserveHost On
        ProxyPass / http://localhost:8080/
        ProxyPassReverse / http://localhost:8080/
</VirtualHost>

but it did not work. I have tweaked the code from top to bottom but my apache server keeps throwing errors. My Apache version is 2.4.52

Note: It is neccessary i do this in apache's vhost and not in the .htaccess file

Please help. thanks in advance.


Solution

  • I finally got it to work.

    The first mistake i made was forgetting to enable mod_rewrite, mod_proxy and mod_proxy_http.

    second mistake was i created two vhost with the same ServerName value, apache seems to ignore one of them

    After enabling those apache modules, here is the final result that worked for me

    <VirtualHost *:80>
        ServerName myservername.test
        #
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-d  
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond "%{REQUEST_URI}" "=/web" [OR]    
        RewriteCond "%{REQUEST_URI}" "=/api"
        RewriteRule . index.php [QSA]
        #
        ProxyPass "/"  "http://localhost:80/"
        ProxyPassReverse "/"  "http://localhost:80/"
    </VirtualHost>