【问题标题】:Apache mod_rewrite not persisting the nameApache mod_rewrite 不保留名称
【发布时间】:2012-05-04 04:41:19
【问题描述】:

我通常将我的 mod_rewrite 条件放在一个 .htaccess 文件中,但在这种情况下,它必须放在 httpd.conf 文件中。 我很困惑,因为我想做的事情似乎很简单: 该站点的根目录是一个嵌套目录:mydomain.com/foo/bar/ 它必须是那样的。 我想写一个规则,以便一个人可以输入: mydomain.com/simple 它将显示来自 mydomain/foo/bar 的内容 此外,如果有人在网站周围点击,我希望 mydomain.com/simple/some-other-page 结构保持不变。 我得到的最接近的是:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule   ^simple$ /foo/bar/$1 [PT]
</IfModule>

但是,使用此规则,当用户键入 mydomain.com/simple 时,它​​会将浏览器中的 URI 重写为 mydomain.com/foo/bar 我究竟做错了什么? 提前致谢。

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:

    首先,这条规则可能有问题:

    RewriteRule   ^simple$ /foo/bar/$1 [PT]
    

    表达式^simple 可能永远不会匹配,因为所有请求都以/ 开头。

    您在规则的右侧使用$1,但左侧没有匹配组来填充它。这意味着对/simple 的请求将获得/foo/bar/,但对/simple/somethingelse 的请求与规则不匹配。如果这不是您想要的行为,您可能是这个意思:

        RewriteRule   ^/simple(.*)$ /foo/bar$1 [PT]
    

    (请注意,我在这里也添加了缺少的前导 /)。

    有了这些更改,我认为此规则在我的系统上的行为符合您的预期。

    最后,打开 RewriteLog 并设置 RewriteLogLevel(假设 Apache 是 2.4 之前的版本)将有助于揭示正在发生的事情的详细信息。

    【讨论】:

      猜你喜欢
      • 2013-10-15
      • 1970-01-01
      • 2015-09-17
      • 2015-02-01
      • 2019-03-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多