【问题标题】:Not Found when rewriting using same rule as another rewrite使用与另一个重写相同的规则重写时未找到
【发布时间】:2014-10-18 00:03:36
【问题描述】:

所以我有一个重写 article?id=10article/10 ,我正在尝试做另一个这样的。

我想从site?=website.com 转到site/website.com。好吧,这似乎是一件容易的事。所以我这样做了

RewriteRule ^site/([0-9]+)$ site.php?id=$1

这与article 重写的规则基本相同。现在我去site/10,例如,我得到了

找不到

在此服务器上找不到请求的 URL /site/sdfsd。

如果它与另一个运行良好的重写相同,为什么会发生这种情况?....

选项 -MultiViews DirectoryIndex index.php 重写引擎开启

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)$ article.php?id=$1 [L,QSA,NC]
RewriteRule ^site/([0-9]+)$ site.php?id=$1

# rewrite from /dir/file/ to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

【问题讨论】:

  • 你确定site?=website.com是正确的吗?

标签: php regex .htaccess mod-rewrite url-rewriting


【解决方案1】:

试试

RewriteRule ^site/(.+)$ site.php?id=$1 [L]

[0-9]+ 只匹配数字。

【讨论】:

  • 用我所有的代码检查我的编辑。我得到了Undefined index: s,所以它不起作用。
  • 你从哪里得到Undefined index: s?这看起来好像来自 php,所以重定向是有效的。
  • 您知道,如果您打开site/website.com,您将被重定向到site.php?id=website.com?没有字符串到 ID 的转换可能(仅当您将其硬编码为单独的重写规则时)。
【解决方案2】:

你似乎有两个问题。

[0-9] 只匹配数字,因此它不适用于字母。

您还需要在规则中添加 [L] 标志,以确保它在第一次匹配时停止。

RewriteRule ^site/(0-9a-zA-Z+)$ site.php?id=$1 [L]

【讨论】:

  • 仍然出现同样的错误。我将所有代码都添加到帖子中
【解决方案3】:

您的规则不匹配,因为角色只允许数字。换句话说,您的规则说:

以数字 (0-9) 后的“site/”开头。

你的规则必须是

RewriteRule ^site/([0-9a-zA-Z]+)$ site.php?id=$1

【讨论】:

    【解决方案4】:

    基本上你的重写规则很糟糕

    RewriteRule ^site/([0-9]+)$ site.php?id=$1 只会匹配数字

    你可以改用

    RewriteRule ^site/(.*)$ site.php?id=$1

    (通配符)

    我不得不说“site/whatever.com”这个表达实际上指向了

    site.php?id=whatever.com - 反向陈述是错误的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 2015-09-09
      • 2013-02-09
      • 2015-12-27
      • 2012-07-22
      • 2013-07-14
      • 2012-04-01
      相关资源
      最近更新 更多