【问题标题】:Rewrite rule not working as expected重写规则未按预期工作
【发布时间】:2013-08-11 08:33:07
【问题描述】:

这是我的 .htaccess 代码,

<IfModule mod_rewrite.c>
RewriteEngine On

#RewriteCond %{REQUEST_URI} /view-([a-zA-Z0-9_-]+)/$
RewriteRule ^view-([a-zA-Z0-9._-]+)/$ post.php?id=$1

</IfModule>

即 id=$1 = my-first-&-thread++ 来自 post.php 文件。

链接变成

http://site.com/view-my-first-&-thread++/

并给出错误 404。

我希望我的链接是这样的:

http://site.com/view-my-first-thread/

这怎么可能?请帮忙

【问题讨论】:

  • 如果你告诉我们你想做什么会更容易,例如将我丑陋的网址http://domain.com/post.php?id=blabla&amp;1203重定向到http://domain.com/view-my-first-thread/1203
  • 我想从 URL 中删除 +、$、& 等字符。
  • 目前还不清楚这里要问什么。起始 URL 是什么,您希望它成为什么目标?如果您删除 +,$,&amp; etc. from URL's,那么内部 URL 将变为 /post.php?id=view-my-first-thread 而不是 /post.php?id=view-my-first-&-thread++` 会有什么帮助? post.php 不是根据传递的 GET 参数 id 进行查找吗?

标签: regex apache .htaccess mod-rewrite


【解决方案1】:

您在RewriteRule 中使用的正则表达式与您的示例网址不匹配,因为它同时排除了+&amp;。有时,通过允许特定字符,您可以隐式排除您可能需要的其他字符。

尝试更改规则以匹配任何内容除了斜线 (/):

RewriteRule ^view-([^/]+)/$` post.php?id=$1

【讨论】:

    【解决方案2】:

    您将需要mod_rewriteB 标志。

    RewriteRule ^view-([^/]+)/?$ post.php?id=$1 [L,QSA,NC,B]
    

    这将重定向到http://site.com/view-my-first-&amp;-thread++/http://site.com/post.php?id=my-first-&amp;-thread++

    【讨论】:

      猜你喜欢
      • 2020-01-12
      • 2016-06-10
      • 1970-01-01
      • 2015-02-03
      • 2018-08-12
      • 2013-07-24
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多