php - .htaccess 用查询字符串重写不同页面的 URL

标签 php regex apache .htaccess mod-rewrite

我有一个旧页面,用于根据查询字符串显示多个产品。我想将带有查询字符串的 url 重写为与旧 url 将被重写到的静态页面不同的静态页面。

   RewriteRule ^old.php$ new.php

   RewriteRule old.php?product=10 different.php [R=301,L]

在这种情况下,old.php 被重写为 new.php。但是 old.php?product=1O 被重写为 new.php?product=10 而不是 different.php

最佳答案

您无法在重写规则中匹配 QUERY_STRING。您需要使用 RewriteCond %{QUERY_STRING} 来匹配查询字符串。

使用您的规则:

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^old\.php$ new.php [L,R]

RewriteCond %{QUERY_STRING} ^product=10$
RewriteRule ^old\.php different.php [L,R]

关于php - .htaccess 用查询字符串重写不同页面的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20656312/

相关文章:

php - Array_merge 与 +

javascript - 使用 Javascript 动态创建的输入不显示下拉列表

php - 智能抓取第一段/起始文本

apache - 在 Windows 中启用 Apache 中的 mod_headers

php - 使用 NuSOAP 检索结果时出现 XML 错误(无效字符)

javascript - 用js发送请求到php

ruby 正则表达式不止一次找到模式

java - 正则表达式模式表达式

php - 删除 index.php url 重写 .htaccess

python - django的wsgi问题