让 CGI 脚本使用 Apache 捕获对域的所有请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/881319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:37:52  来源:igfitidea点击:

Having a CGI script catch all requests to a domain with Apache

apachemod-rewriteapache2cgi

提问by Jotham

Using Apache 2, I want to configure my website so that any requests to the domain are forwarded to a Python CGI script. Basically, if the user goes to http://www.example.comi want the cgi /cgi-bin/cgi.py to execute. If the user goes to http://www.example.com/index.rssI want /cgi-bin/cgi.py to be executed with /index.rss as the argument. I have tried various combinations of ScriptAlias and Rewrite and cannot seem to get them in the right relationship.

使用 Apache 2,我想配置我的网站,以便对域的任何请求都转发到 Python CGI 脚本。基本上,如果用户访问http://www.example.com,我希望执行 cgi /cgi-bin/cgi.py。如果用户访问http://www.example.com/index.rss,我希望 /cgi-bin/cgi.py 以 /index.rss 作为参数执行。我尝试了 ScriptAlias 和 Rewrite 的各种组合,但似乎无法让它们处于正确的关系中。

采纳答案by Jotham

(not sure on the correct procedure with answering ones own question - but...)

(不确定回答自己问题的正确程序 - 但是......)

Looks like I was having conflict with ScriptAlias and RewriteRule. In the end the solution was to use AddHandler to create a relationship then use mod_rewrite to pull everything into the CGI. And RewriteCond to avoid catching /resources/ and /media/. My VirtualHost now looks like this:

看起来我与 ScriptAlias 和 RewriteRule 有冲突。最后的解决方案是使用 AddHandler 创建关系,然后使用 mod_rewrite 将所有内容拉入 CGI。和 RewriteCond 以避免捕获 /resources/ 和 /media/。我的 VirtualHost 现在看起来像这样:

AddHandler cgi-script .cgi
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/resources/.*$
RewriteCond %{REQUEST_URI} !^/media/.*$
RewriteRule ^(.*)$ /cgi-bin/pyblosxom.cgi [L]

Thanks for your help guys.

谢谢你们的帮助。

回答by Greg

RewriteEngine On

RewriteRule ^(.*)$ /cgi-bin/cgi.py?url=

This will redirect ALL requests to your python file. If you're having trouble with the script alias still, try adding the passthrough flag [PT] at the end of the RewriteRule line If you still want to be able to access images etc then add this before the RewriteRule:

这会将所有请求重定向到您的 python 文件。如果您仍然无法使用脚本别名,请尝试在 RewriteRule 行的末尾添加直通标志 [PT] 如果您仍然希望能够访问图像等,请在 RewriteRule 之前添加:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

回答by Fuzzy76

While it's not 100% what you're looking for, here's the .htaccess I use for an old abandoned domain of mine to redirect people properly. It basically redirects for any file or directory not found in the local directory structure. It's up to the script itself to figure out what url it was called for:

虽然它不是 100% 您正在寻找的内容,但这是我用于我的旧废弃域以正确重定向人们的 .htaccess。它基本上重定向本地目录结构中未找到的任何文件或目录。由脚本本身来确定它被调用的 url:

RewriteEngine On

#if the request isn't for a file or a directory...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

回答by Brad

I used the capturing rewrite rule and it worked to a degree. The problem was that original query string of the request URI wasn't passed to the cgi when using $1. I ended up removing the capture and just referencing ENV['REQUEST_URI'] in my cgi script to gain access.

我使用了捕获重写规则,它在一定程度上起作用。问题是在使用 $1 时,请求 URI 的原始查询字符串没有传递给 cgi。我最终删除了捕获并仅在我的 cgi 脚本中引用 ENV['REQUEST_URI'] 以获得访问权限。