【问题标题】:Rewrite and redirect a url without query string (static) to url with query string parameter将不带查询字符串(静态)的 url 重写并重定向到带查询字符串参数的 url
【发布时间】:2015-09-10 17:14:30
【问题描述】:

假设我有一个网址

http://www.naturaflowers.com/products_new.html

我想重写并重定向到

http://www.naturaflowers.com/products_new.html?page=1

因为两个 url 打开同一个页面。我尝试了以下但没有工作

RewriteCond %{REQUEST_URI} ^/products_new.html [NC]
RewriteRule /products_new.html?page=1 [R=301,L]

请帮我解决这个问题。谢谢。

【问题讨论】:

  • 为什么每次有人访问页面时都想添加相同的查询字符串?难道你不能假设如果没有查询字符串比用户在第 1 页上?
  • @samrap 但怎么可能。在这种情况下,两个 url 打开同一个页面。我想从另一个重定向一个。请你帮帮我。

标签: php regex .htaccess mod-rewrite


【解决方案1】:

您可以在根 .htaccess 中将此规则用作第一个规则

RewriteCond %{QUERY_STRING} !(^|&)page=1\b [NC]
RewriteRule ^products_new\.html$ %{REQUEST_URI}?page=1 [R=301,L,QSA,NC]

需要RewriteCond 以确保?page=1 不存在。

【讨论】:

  • @anubhava 我需要与此相反。有没有人知道怎么做?
  • @Mahesh:发布一个新问题,我会尽力帮助你。
猜你喜欢
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
  • 2018-10-24
  • 2014-07-08
  • 2019-06-17
  • 2020-12-14
相关资源
最近更新 更多