问题原文
原帖链接 · Dorado · CC BY-SA 4.0
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.
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
If you want to put those directives directly inside the VirtualHost container (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
RewriteRulepattern is root-relative, starting with a slash. So, instead of simply.in.htaccess, you need/.(or^/.) in the vhost config.The
RewriteBasedirective does not apply in a virtualhost context, so needs to be removed. (Although you weren't making use of this anyway in your existing.htaccessfile.)
Additional:
In the directive that sets the
HTTP_AUTHORIZATIONenvironment variable, the pattern.*is less efficient. Better to use^(or similar - something that does not have to traverse the entire URL-path) instead.Your initial
<IfModule>wrapper is meaningless.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
