mod_rewrite:重写URL以指向cgi脚本,但保持重写隐藏

时间:2015-05-06 01:01:01

标签: apache .htaccess mod-rewrite url-rewriting

我试图重写网址

http://domain.com/whatever 

要:

http://domain.com/cgi-bin/script.cgi

我的.htaccess文件如下所示:

RewriteEngine On
RewriteRule /whatever /cgi-bin/script.cgi [NC]

这不起作用并且给我404错误。但是,这有效:

RewriteEngine On
RewriteRule /whatever http://domain.com/cgi-bin/script.cgi [NC]

但在这种情况下,URL更改对用户是可行的。我做错了什么?

2 个答案:

答案 0 :(得分:2)

添加[PT]标志修复它:

RewriteEngine On
RewriteRule /whatever /cgi-bin/script.cgi [NC,PT]

现在,如果有人能够解释为什么需要这样做以及实际发生了什么,我将非常感激。

答案 1 :(得分:0)

我在使用 apache 重写引擎时遇到了与非 PHP CGI 相同的问题。 一切正常,但未添加查询字符串...

要解决它,您必须在重写配置中设置“QSA”参数

DocumentRoot "${INSTALL_DIR}/www"
<Directory "${INSTALL_DIR}/www/">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Multiviews Includes ExecCGI

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
#   AllowOverride FileInfo AuthConfig Limit
#
AllowOverride all

#
# Controls who can get stuff from this server.
#
AddHandler cgi-script .vbs .wsf .rex .bat .cmd .bas .rexg
#   onlineoffline tag - don't remove

#Require local

RewriteEngine   on

#RewriteRule "(.*\.vbs)$" "/cgi/test.rex"
#[H=cgi-script]
# AVOID RECURSION BY CONDITION !!!
RewriteCond  "%{REQUEST_FILENAME}" "!(.*)/cgi\.rex" 
RewriteRule "(.*\.rex)$" "/cgi/cgi.rex?$1" [NC,PT,QSA]
RewriteRule "(.*\.vbs)$" "/cgi/cgi.rex?$1" [NC,PT,QSA]
RewriteRule "(.*\.rsp)$" "/cgi/cgi.rex?$1" [NC,PT,QSA]



#RewriteLog      "/var/log/apache2/rewrite.log"
#RewriteLogLevel 2



</Directory>

在此更改后,查询字符串环境又回来了:)