【问题标题】:Apache rewrite condition and the absence of charactersApache 重写条件和缺少字符
【发布时间】:2023-04-04 22:21:01
【问题描述】:

我们有以下 mod_rewrite 条件/规则,但如果 URL 仅以“i”(或“css”开头)不完全等同于我们相应的目录(/i /css),它将返回 404 错误等等)

RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|/(i|css|design_and_docs|js|test)/*)
RewriteRule ^(.*)$ /index.php/$1 [L]

这使我无法拥有一个路由/URI(使用 CodeIgniter,但我认为这并不重要),例如 /itemid 或 /idcommand(只是我在过去一周需要的几个)

我可以按如下方式轻松修改 RewriteCond 以测试尾部斜杠,但随后对 /i 目录的请求(没有尾部斜杠)失败,而之前它可以工作:

RewriteCond $1 !^(index\.php|robots\.txt|/(i|css|design_and_docs|js|test)\//*)

使用尾部斜杠 /i/ 仍然可以解析,这对于我们的目的可能已经足够了。但我真的很希望 /i 仍然解析到相应的 /i 目录。因此,我不只是在末尾测试斜杠,我想测试斜杠,是否缺少任何字符。

如何用正则表达式做到这一点?

【问题讨论】:

    标签: regex apache mod-rewrite


    【解决方案1】:

    不只是 \/ 作为尾部斜杠,而是做一个子组并检查那个或字符串的结尾,这是一个 $:(\/|$)

    RewriteCond $1 !^(index\.php|robots\.txt|/(i|css|design_and_docs|js|test)(\/|$)/*)
    

    【讨论】:

    • 谢谢你成功了。我知道 $“anchor”,但不知道您可以在子组中使用它。
    【解决方案2】:

    我不确定/ 是否符合您的预期。 AFAIK 你不需要在.htaccess 中使用前导斜杠。你有没有试过这个:

    RewriteCond $1 !^(index\.php|robots\.txt|i/|css/|design_and_docs/|js/|test/)
    

    【讨论】:

      猜你喜欢
      • 2014-01-17
      • 2012-12-25
      • 1970-01-01
      • 2010-11-16
      • 2013-11-12
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多