【问题标题】:Help with mod_rewrite rule for dynamic url帮助动态 url 的 mod_rewrite 规则
【发布时间】:2009-05-25 20:35:02
【问题描述】:

呃.. mod_rewrite 让我觉得自己很愚蠢。我只是还没想好。 :/

我有这个网址:

http://example.com/a/name/

...我想在这里指出:

http://example.com/a/index.php?id=name

...其中name 是作为id 参数传递给index.php 的内容。

我尝试过的任何结果都是 404 或 500.. :(

【问题讨论】:

    标签: apache mod-rewrite dynamic-url


    【解决方案1】:

    如果您希望尾部斜杠是可选的,则必须排除您正在将请求重写到的文件。否则你将有一个很好的无限递归。

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/a/index\.php$
    RewriteRule ^/a/([^/]+)/?$ /a/index.php?id=$1 [L]
    

    这里任何以/a/… 开头但不是/a/index.php 的请求都被重写为/a/index.php

    但如果尾部斜杠是强制性的,则不需要排除目标文件:

    RewriteEngine on
    RewriteRule ^/a/([^/]+)/$ /a/index.php?id=$1 [L]
    

    【讨论】:

      【解决方案2】:

      让你开始:

      RewriteEngine on
      RewriteCond %{REQUEST_FILENAME} !index.php
      RewriteRule ^/?a/([^/]+)/?$ /a/index.php?id=$1 [QSA,L]
      

      如果一个重写教程不适合你,try another

      编辑:根据 Gumbo 的建议排除 index.php

      【讨论】:

      • 导致 500 错误:由于可能的配置错误,请求超出了 10 次内部重定向的限制。
      • 啊,这只是因为它试图重写重写的 URL.. 如果我从 ^/?aa/ 开始,那么它工作正常。谢谢!
      • @Ian 你必须排除你正在重写请求的文件。
      【解决方案3】:

      大概是这样的

      RewriteEngine on
      RewriteBase /a/
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)/?$ index.php?id=$1 [L,QSA]
      

      会成功的。

      【讨论】:

        【解决方案4】:

        我建议你看看这个网址:

        http://www.dracos.co.uk/code/apache-rewrite-problem/

        提供的解决方案将起作用,但 URL 中解释了一些注意事项,主要是关于 ?和 # 在 URL 本身中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-13
          • 1970-01-01
          • 1970-01-01
          • 2014-01-29
          相关资源
          最近更新 更多