有一个 CGI 脚本为我们的客户提供一些 API。调用语法为:
script.cgi?module=<str>&func=<str>[&other-options]
任务是为不同的模块制定不同的认证规则。
或者,拥有漂亮的 URL 会很棒。
我的配置:
<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName example.com
# Global policy is to deny all
<Location />
Order deny,allow
Deny from all
</Location>
# doesn't work :(
<Location /api/foo>
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>
RewriteEngine On
# The only allowed type of requests:
RewriteRule /api/(.+?)/(.+) /cgi-bin/api.cgi?module=$1&func=$2 [PT]
# All others are forbidden:
RewriteRule /(.*) - [F]
RewriteLog /var/log/apache2/rewrite.log
RewriteLogLevel 5
ScriptAlias /cgi-bin /var/www/example
<Directory /var/www/example>
Options -Indexes
AddHandler cgi-script .cgi
</Directory>
</VirtualHost>
好吧,我知道问题是处理该指令的顺序。<Location>
s 将在 mod_rewrite 完成工作后处理。但我相信有办法改变它。:)
最好使用标准Order deny,allow
+Allow from <something>
指令,因为它在其他地方也很常用。
感谢您的关注。:)