Working with rewrite module in apache vhost file

Discussion in 'Server Operation' started by thisiszeev, Oct 29, 2024 at 11:12 AM.

  1. thisiszeev

    thisiszeev Member HowtoForge Supporter

    I want to setup my vhost file, that when a folder is accessed, it opens, executes and serves back to the browser the out put of default.cgi

    I can get that working, no problem, but I want it also setup so that if the client tries to access any file on the server directly, including default.cgi, it must serve back the document /404.cgi

    Here is a copy of my current vhost file that happily serves default.cgi... but when I tried enable one of the rules that I have commented out, then everything I do just returns Error 500.

    Code:
    <VirtualHost *:80>
        ServerName api.mydomain.tld
    
        DocumentRoot /home/api/endpoints
    
        <Directory "/home/api/endpoints">
            Options +ExecCGI
            AddHandler cgi-script .cgi
            AllowOverride All
    
            RewriteEngine On
            RewriteBase /
    
            # Serve default.cgi for any folder access
            RewriteCond %{REQUEST_FILENAME} -d
            RewriteRule ^(.*)/?$ $1/default.cgi [L]
    
    #        RewriteCond %{REQUEST_FILENAME} !-d
    #        RewriteRule ^(.*)/?$ /404.cgi [L]
    
    #        # Catch-all rule for any other requests not matching previous conditions
    #        RewriteRule ^(.*)$ /404.cgi [L]
    
            Require all granted
        </Directory>
    
        # Custom error document (optional, if you want to specify a 404 error page)
        ErrorDocument 404 /404.cgi
    
        # Log locations
        ErrorLog /home/api/log/apache_error.log
        CustomLog /home/api/log/apache_access.log combined
    </VirtualHost>
    
     
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    What is the 500 error message in the log?
     
  3. thisiszeev

    thisiszeev Member HowtoForge Supporter

    From a blank error.log, it generates a massive file from one http request.

    I think it is hitting a race condition.

    So I am now trying to research how to rather have a folder structure that is void of files, but when you got / or /endpoint or whatever, then it executes the file default.cgi from an external location. Say, /home/api/code/default.cgi or /home/api/code/endpoint/default.cgi
     

Share This Page