Working with rewrite module in apache vhost file

Discussion in 'Server Operation' started by thisiszeev, Oct 29, 2024.

  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
     
  4. Alex Mamatuik

    Alex Mamatuik Member

    i think, you have to examine the way the Drupal handles it:

    .htaccess
    Code:
    # Make Drupal handle any 404 errors.
    ErrorDocument 404 /index.php
    index.php
    Code:
    define('DRUPAL_ROOT', getcwd());
    
    require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    menu_execute_active_handler();
    bootstrap.inc
    Code:
    function drupal_fast_404() {
      $exclude_paths = variable_get('404_fast_paths_exclude', FALSE);
      if ($exclude_paths && !preg_match($exclude_paths, $_GET['q'])) {
        $fast_paths = variable_get('404_fast_paths', FALSE);
        if ($fast_paths && preg_match($fast_paths, $_GET['q'])) {
          drupal_add_http_header('Status', '404 Not Found');
          $fast_404_html = variable_get('404_fast_html', '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>404 Not Found</title></head><body><h1>Not Found</h1><p>The requested URL "@path" was not found on this server.</p></body></html>');
          // Replace @path in the variable with the page path.
          print strtr($fast_404_html, array('@path' => check_plain(request_uri())));
          exit;
        }
      }
    }
    
     
  5. nanaveda

    nanaveda New Member

    Can you share the output of this?
     
  6. Alex Mamatuik

    Alex Mamatuik Member

    ... and a portion of ayurveda, Sir! ;):D:)
     
  7. thisiszeev

    thisiszeev Member HowtoForge Supporter

    Sorry, been hectic busy this side of the planet.

    Here is the working vhost file:

    Code:
    <VirtualHost *:80>
        ServerName api.mydomain.tld
        DocumentRoot /home/api/wwwroot
    
        <IfModule mod_suexec.c>
            SuexecUserGroup api api
        </IfModule>
    
        <Directory "/home/api/wwwroot">
            Options +ExecCGI
            AddHandler cgi-script .cgi
            AllowOverride All
    
            RewriteEngine On
    
            # Avoid rewriting if the request is already for default.cgi
            RewriteCond %{REQUEST_URI} !^/default\.cgi$
            RewriteCond %{REQUEST_URI} !^/400\.cgi$
            RewriteCond %{REQUEST_URI} !^/401\.cgi$
            RewriteCond %{REQUEST_URI} !^/403\.cgi$
            RewriteCond %{REQUEST_URI} !^/404\.cgi$
            RewriteCond %{REQUEST_URI} !^/500\.cgi$
            RewriteCond %{REQUEST_URI} !^/502\.cgi$
            RewriteCond %{REQUEST_URI} !^/503\.cgi$
            RewriteCond %{REQUEST_URI} !^/504\.cgi$
    
            # Route all other requests to default.cgi and pass URI as a path variable and query as QUERY_DATA
            RewriteRule ^(.*)$ /default.cgi?path=$1 [QSA,L]
    
            Require all granted
        </Directory>
    
        # Custom error document
        ErrorDocument 400 /400.cgi
        ErrorDocument 401 /401.cgi
        ErrorDocument 403 /403.cgi
        ErrorDocument 404 /404.cgi
        ErrorDocument 500 /500.cgi
        ErrorDocument 502 /502.cgi
        ErrorDocument 503 /503.cgi
        ErrorDocument 504 /504.cgi
    
        # Log locations
        ErrorLog /home/api/log/apache_error.log
        CustomLog /home/api/log/apache_access.log combined
    </VirtualHost>
    
     
    ahrasis likes this.

Share This Page