.htaccess 重写规则在传递多个变量时导致错误 404

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

当我使用 .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>

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

最佳答案

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

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

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

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

关于.htaccess 重写规则在传递多个变量时导致错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25042495/

相关文章:

php - 共享 php 脚本的别名、mod_rewrite 或其他名称?

php - 重定向而不更改浏览器 url

.htaccess - ErrorDocument 404 打印文件名

java - HTTP 状态 - 在 JSP 中找不到 404 资源异常

apache - 允许访问 Apache 服务器上隐藏目录中的文件(尤其是 ".well-known"文件夹)

php - 关于 PHP 语法错误的 Apache 内部错误 500 (htaccess)

.htaccess : if is an path, 什么都不做,否则,做重写规则

c# - 在 IIS 7.5 中托管的 Web Api 中找不到 HTTP 404 页面

.htaccess - 将带有FQDN的URL(TLD后加点)重定向为与PQDN等效的URL

.htaccess 重写而不影响相对的 image/css/js URL?