【问题标题】:mod_rewrite: Rewrite URL to point to cgi script, but keep rewrite hiddenmod_rewrite:重写 URL 以指向 cgi 脚本,但保持重写隐藏
【发布时间】:2015-05-06 01:01:01
【问题描述】:

我正在尝试重写 URL

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 更改对用户来说是可见的。我做错了什么?

【问题讨论】:

  • 你还有其他规定吗?重写规则的模式也没有前导斜杠(用于匹配的 URI 已删除前导斜杠)
  • 没有其他规则。这些是 .htaccess 文件的完整内容。我不确定你对前导斜线的意思,但第二个示例可以执行重定向,所以我不认为这是模式的问题......
  • 已编辑:没关系,我错了。

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


【解决方案1】:

添加[PT] 标志修复它:

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

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

【讨论】:

  • passthrough|PT - 强制将生成的 URI 传递回 URL 映射引擎,以处理其他 URI 到文件名的转换器,例如别名或重定向。默认情况下,RewriteRule 中的目标(或替换字符串)假定为文件路径。使用 [PT] 标志会导致它被视为 URI。也就是说,使用 [PT] 标志会导致 RewriteRule 的结果通过 URL 映射传回,因此基于位置的映射,例如 Alias、Redirect 或 ScriptAlias,可能有机会生效。
  • 如果,例如,你有一个 /icons 的别名,并且有一个 RewriteRule 指向那里,你应该使用 [PT] 标志来确保别名被评估。在这种情况下省略 [PT] 标志将导致别名被忽略,从而导致返回“未找到文件”错误。
  • 那么可能你有一些重定向或别名应该应用于重写的 URL?
【解决方案2】:

我在使用 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>

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

【讨论】:

    猜你喜欢
    • 2012-05-30
    • 2011-09-23
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2012-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多