1

I have the following .htaccess file:

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
</IfModule>

RewriteBase /
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /API/index.php [L]

Is it possible to convert the above rules into usable VirtualHost block?

<VirtualHost *:80>
    ServerAdmin webmaster@web-api
    DocumentRoot "/Users/shot/git/web-api"
    ServerName web-api
    ServerAlias web-api
    ErrorLog "/private/var/log/apache2/web-api-error_log"
    CustomLog "/private/var/log/apache2/web-api-access_log" common
    
    RewriteEngine on

    <Directory "/Users/shot/git/web-api">
        Options FollowSymLinks
        Order allow,deny
        Allow from all
        Require all granted
        RewriteBase /
        RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /API/index.php [L]
    </Directory>
</VirtualHost>

But in apache error log, I get the following output [core:error] [pid 2520] [client 127.0.0.1:52625] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

1
  • I've answered before your subsequent edit... you had stated "inside the VirtualHost block" - which is how I've answered. However, I see you are putting the directives inside a <Directory> section - this is very different. Is this a requirement? Note that .htaccess will override this.
    – MrWhite
    Jul 6, 2020 at 17:07

1 Answer 1

1

If you want to put those directives directly inside the VirtualHost (ie. not inside a <Directory> container) then you could rewrite them like so:

Options +FollowSymlinks
RewriteEngine On

RewriteRule ^ - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteRule ^/. /API/index.php [L]

Required changes:

  • When the vhost directives are processed, the request has not yet been mapped to the filesystem, hence the need to use a look-ahead (LA-U:REQUEST_FILENANE) to get the resulting filename.

  • In a virtualhost context, the URL-path matched by the RewriteRule pattern is root-relative, starting with a slash. So, instead of . in .htacces, you need /. (or ^/.) in the vhost config.

  • The RewriteBase directive does not apply in virtualhost context, so needs to be removed. (Although you weren't making use of this anyway in your existing .htaccess file.)

Additional:

  • In the directive that set the HTTP_AUTHORIZATION environment variable, the pattern .* is less efficient. Better to use ^ (or similar - something that does not have to traverse the entire URL-path)

  • Your initial <IfModule> wrapper is meaningless.

4
  • I appreciate your response. I tried your example and 404 is returned with postman at GET http://web-api/admin/order. This is behind Slim/Symfony framework; the URL is mapped to one of the PHP files. Am I missing something?
    – Dorado
    Jul 6, 2020 at 17:26
  • Also, if I used /. instead of ^/., I get the same Request exceeded the limit of 10 internal redirects error.
    – Dorado
    Jul 6, 2020 at 17:34
  • To clarify, the directives I posted above you put directly inside the <VirtualHost> container, not inside the <Directory> section? Presumably .htaccess has been removed? What happens when you request /API/index.php directly? You can put a specific check in for /API/index.php (which would possibly be marginally more efficient) - but it should "work" without.
    – MrWhite
    Jul 6, 2020 at 17:44
  • I figured it out. Running php server was the answer. Your solution helped me to move in right direction. In end, I still need the .htaccess file. I'm going to mark your answer as correct because it's how conversion should be done.
    – Dorado
    Jul 6, 2020 at 19:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .