大网数据广告活动
服务器运维问答

Rewrite Rule from .htaccess into Apache VirtualHost

· 52CE 我爱测

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 …

问题原文

原帖链接 · 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 RewriteRule pattern is root-relative, starting with a slash. So, instead of simply . in .htaccess, you need /. (or ^/.) in the vhost config.

  • The RewriteBase directive does not apply in a 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 sets 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) instead.

  • Your initial <IfModule> wrapper is meaningless.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-07-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。