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