【问题标题】:.htaccess rewritecond rewrite case level 2 folder=info.htaccess rewritecond rewrite case level 2 folder=info
【发布时间】:2015-05-04 03:50:39
【问题描述】:

我想设置.htaccess重写:

example.com/bla/info/

example.com/info/index.php?q=bla

example.com/bla/info/txt/

example.com/info/index.php?q=bla&t=txt

我希望浏览器仍然显示:

example.com/bla/info/txt/

我不想在 2 级重写中重写,例如:

example.com/bla/xxx/ 

example.com/ccc/zzz/aaa/

但是

example.com/silly/info/ 

效果很好

example.com/strange/info/mytxt/

这有意义吗?

有什么帮助吗?

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    如果您的模式以 ^ 开头并以 $ 结尾,则该规则仅在整个字符串匹配时才适用。

    RewriteRule   ^/bla/info/$      /info/index.php?q=bla
    RewriteRule   ^/bla/info/txt/$  /info/index.php?q=bla&t=txt
    

    如果您使用不使用 [R] 选项,则浏览器中显示的 URL 将是用户输入的任何内容。

    您是否正在尝试将此通用化?如果是这样,您可以使用正则表达式类型匹配和变量替换。要使 'bla' 匹配直到下一个斜杠 (/) 的任何字符串,请使用

    ([^/]+)
    

    在你的模式中并在你的替换中用 $1 引用它。

    RewriteRule ^/([^/]+)/info/$          /info/index.php?q=$1
    RewriteRule ^/([^/]+)/info/([^/]+)/$  /info/index.php?q=$1&t=$2
    

    我推荐Apache web pages about mod_rewrite 了解更多信息。

    [编辑。修复了在模式中不包含主机名的规则,正如原始发布者所发现的那样。]

    --
    bmb

    【讨论】:

      【解决方案2】:

      你让我走上了正轨。

      我最终得到了这样的结果:

      RewriteRule ^([^/\.]+)/info/?$ example/info/index.php?q=$1 [L]
      

      非常感谢

      【讨论】:

      • 糟糕,我忘记了主机名没有出现在模式中。我确定了答案。
      猜你喜欢
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多