简体   繁体   English

Perl CGI的漂亮网址

[英]Pretty URL for Perl CGI

I just converted over from using PHP to Perl for a script that hosts API commands. 我只是从使用PHP转换为Perl来托管API命令的脚本。

As php, the .htaccess looked like: 作为php,.htaccess如下所示:

RewriteRule ^api/?$ api.php [L]
RewriteRule ^api/?([a-zA-Z0-9_-]{1,})?/?$ api.php?command=$1 [QSA,L]
RewriteRule ^api/?([a-zA-Z0-9_-]{1,})?/?([a-zA-Z0-9_-]{1,})?/?$ api.php?command=$1&key=$2 [QSA,L]

End result was executing with a url like http://example.com/api/command/key (where api is the script name), worked as intended. 最终结果执行时使用了预期的效果,例如使用http://example.com/api/command/key (其中api是脚本名称)之类的网址。

Switching over to Perl, visiting http://example.com/api redirects to http://example.com/usr/lib/cgi-bin/api.pl which doesn't work in the slightest, and also looks hideous. 切换到Perl,访问http://example.com/api重定向到http://example.com/usr/lib/cgi-bin/api.pl ,这一点都不起作用,而且看起来也很丑。 I've tried everything I can think of with .htaccess and serve-cgi-bin.conf , but nothing works for me. 我已经尝试过使用.htaccessserve-cgi-bin.conf ,但是对我来说没有任何效果。 It doesn't really matter which folder I need to put the api.pl in, as long as the folder name and extension don't appear in the url. 只要文件夹名称和扩展名不出现在url中,我不需要将api.pl放入哪个文件夹都没有关系。

How can I get the desired url? 如何获得所需的网址?

You should only need to replace the api.php with cgi-bin/api.pl in your rules. 您只需在api.php中用cgi-bin/api.pl替换api.php

RewriteRule ^api/?$ cgi-bin/api.pl [L]
RewriteRule ^api/?([a-zA-Z0-9_-]{1,})?/?$ cgi-bin/api.pl?command=$1 [QSA,L]
RewriteRule ^api/?([a-zA-Z0-9_-]{1,})?/?([a-zA-Z0-9_-]{1,})?/?$ cgi-bin/api.pl?command=$1&key=$2 [QSA,L]

You can use a tool like htaccess.madewithlove.be to test your rules. 您可以使用htaccess.madewithlove.be之类的工具来测试您的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM