AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 104442
Accepted
Insyte
Insyte
Asked: 2010-01-20 19:18:57 +0800 CST2010-01-20 19:18:57 +0800 CST 2010-01-20 19:18:57 +0800 CST

Apache mod_rewrite 规则顺序混乱

  • 772

我正在尝试设置几个简单的规则,这些规则根据路径和查询字符串的位从旧版本的站点重写 URL。

第一次看它看起来像这样:

RewriteEngine on

RewriteMap freeitems txt:/www/www.example.com/etc/freeitems_map.txt
RewriteMap items txt:/www/www.example.com/etc/items_map.txt

RewriteCond %{QUERY_STRING} ^sku=(\d+)$
RewriteRule ^/free_item.php$ https://www.example.com/shop/detail/${freeitems:%1}?
RewriteRule ^/item.php$ https://www.example.com/shop/detail/${items:%1}?

这似乎对/free_item.phpURL 很有效,正确地替换了 的结果RewriteMap,但是/item.phpURL 只返回了重定向 URL 的第一部分,而忽略了RewriteMap查找的结果。如果我颠倒了规则的顺序,失败/工作的 URL 就会颠倒。正如我一直理解RewriteRule的语法一样,如果 URL 路径与模式不匹配,则会跳过规则并继续处理下一个规则。在这种情况下,

我终于设法通过将其分成两个块来使其工作:

RewriteEngine on

RewriteMap freeitems txt:/www/www.example.com/etc/freeitems_map.txt
RewriteMap items txt:/www/www.example.com/etc/items_map.txt

RewriteCond %{QUERY_STRING} ^sku=(\d+)$
RewriteRule ^/free_item.php$ https://www.example.com/shop/detail/${freeitems:%1}?

RewriteCond %{QUERY_STRING} ^sku=(\d+)$
RewriteRule ^/item.php$ https://www.example.com/shop/detail/${items:%1}?

谁能告诉我为什么第一种技术失败了?

编辑:作为测试,我在第二个重定向的 URL 中添加了一个额外的路径元素RewriteRule。当我请求一个应该匹配第二个规则而不是第一个规则的 URL 时,该路径元素会按预期显示。只是不是RewriteMap查找的结果。

谢谢!

-本

apache-2.2 mod-rewrite
  • 1 1 个回答
  • 772 Views

1 个回答

  • Voted
  1. Best Answer
    chris
    2010-01-29T19:29:19+08:002010-01-29T19:29:19+08:00

    第一种技术失败的原因是第二个 RewriteRule 上的 RewriteCond %1 反向引用不存在。每个 RewriteRule 都有自己的一组 RewriteCond,并且不与其他 RewriteRule 块共享 RewriteCond。

    请参阅:http ://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#InternalRuleset

    我还建议添加 [L] 标志,因为看起来每条规则都是独立的。例如:

    RewriteRule ^/free_item.php$ https://www.example.com/shop/detail/${freeitems:%1}? [L]
    
    RewriteRule ^/item.php$ https://www.example.com/shop/detail/${items:%1}? [L]
    

    祝你好运 :)

    • 1

相关问题

  • 阿帕奇的替代品

  • 如何强制我的网址始终以 www 开头?

  • 在 Linux Xen VPS 上优化 Apache 和 MySQL

  • mod_rewrite 不转发 GET 参数

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve