【问题标题】:.htaccess rewrite rule causes an error 404 when multiple variables are passed.htaccess 重写规则在传递多个变量时导致错误 404
【发布时间】:2014-09-22 10:18:52
【问题描述】:

当我使用 .htaccess 重写规则将 /posts/post.php?postid=1 重写为 /posts/1 时,重写工作和 post.php页面加载。

.htaccess 文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /posts/
    RewriteRule ^([a-zA-Z0-9]+)$ post.php?postid=$1
</IfModule>

但是,当我想将 URL 中的多个变量 (/posts/post.php?date=2014-07-28&title=title%20of%20post 传递给 /posts/ 2014-07-28/title%20of%20post) 触发错误 404。

.htaccess 文件:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /posts/
    RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ post.php?date=$1&title=$2
</IfModule>

对此问题的任何帮助将不胜感激并提前致谢。

【问题讨论】:

  • 阅读 QSA 标志。这就是你要找的。​​span>
  • @TerryE 我已经阅读了 QSA 标志,实施了必要的更改并尝试了它,但是它似乎没有工作,它仍然导致错误 404。

标签: .htaccess mod-rewrite http-status-code-404


【解决方案1】:

您的正则表达式不包含 %20 或空格之类的内容,请尝试使其接受所有内容:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /posts/
    RewriteRule ^([a-zA-Z0-9]+)/(.+)$ post.php?date=$1&title=$2
</IfModule>

可能需要使用NE 和/或B 标志,具体取决于您的网址中的编码内容类型:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /posts/
    RewriteRule ^([a-zA-Z0-9]+)/(.+)$ post.php?date=$1&title=$2 [L,NE,B,QSA]
</IfModule>

【讨论】:

    猜你喜欢
    • 2013-11-24
    • 2023-04-08
    • 1970-01-01
    • 2019-03-19
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    相关资源
    最近更新 更多