【问题标题】:htaccess redirect for redirecting all url with page to index.phphtaccess 重定向用于将所有带有页面的 url 重定向到 index.php
【发布时间】:2017-05-25 13:53:38
【问题描述】:

我需要建议使用 htaccess 对所有具有 ?page=9 和 index.php 的页面进行 301 永久重定向

任何人都可以指导如何实现期望

例如-1

http://www.example.com/?page=9&option=com_news&view=list&Itemid=100&limitstart=840

http://www.example.com/index.php&option=com_news&view=list&Itemid=100&limitstart=840

或者比如说2

http://www.example.com/?page=17&option=com_news&view=list&Itemid=100

http://www.example.com/index.php&option=com_news&view=list&Itemid=100

编辑 二手

RewriteEngine on

RewriteCond %{THE_REQUEST} \?page=.+&(.+)\sHTTP [NC]
RewriteRule ^ /index.php&%1? [L,R=301]

但它的作用就像输出一样

http://www.example.com/index.php&limitstart=840

【问题讨论】:

  • 你只是想删除页面?
  • 是删除 ?page 并添加 index.php 反对它
  • index.php 是隐式的(假设您的 DirectoryIndex 已为此设置) - /?something=something/index.php?something=something 之间没有实际区别

标签: php .htaccess


【解决方案1】:

假设您实际上想要/index.php?option=... 而不是/index.php&option=... - 只是匹配查询字符串:

RewriteEngine On
RewriteCond %{QUERY_STRING} page=[^&]+&(.*) [NC]
RewriteRule .* /index.php?%1 [L,R,QSD]

但是,如果您真的确实想要/index.php&option=...,请将 RewriteRule 替换为:

RewriteRule .* /index.php&%1 [L,R,QSD]


注意:无论哪种方式,如果 page 不是第一个参数,这可能无法按预期工作


如果您的 Apache 版本早于 2.4,请将 RewriteRule 替换为:

RewriteRule .* /index.php?%1? [L,R]

[QSD] 标志仅在 2.4 中引入 - 不应该无论如何都是必需的,因为正在匹配整个查询字符串。

【讨论】:

  • 我可以使用 - RewriteCond %{QUERY_STRING} page=[^&]+&(.*) [NC] RewriteRule .* /index.php?%1 [L,R=301]
  • 标志中的RR=301 没有区别,因为默认重定向是301...应该可以; QSD 应该从重写的 URL 中丢弃任何额外的查询字符串 - 因为您匹配整个内容,所以它不应该是必需的,所以是的,您可以将其关闭。在进行查询字符串重写时,我只是对无限递归循环感到偏执。
【解决方案2】:

你可以用这个:

RewriteEngine on

RewriteCond %{THE_REQUEST} \?page=.+&(.+)\sHTTP [NC]
RewriteRule ^ /index.php&%1? [L,R=301]

【讨论】:

  • 技术上这就是所要求的,但我怀疑您将收到 没有工作 评论,因为查询字符串以 & 开头
  • 谢谢,但它确实有效 - 输出为 example.com/index.php&limitstart=840
  • @Ruchika - 不,但你可以尝试\?page=.+?&(.+)\sHTTP,在+ 之后添加? 将其更改为非贪婪匹配......虽然我认为Apache mod_rewrite 是非贪婪的默认情况下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
  • 1970-01-01
  • 2022-01-16
  • 2013-08-26
  • 2016-11-30
  • 2016-11-30
  • 1970-01-01
相关资源
最近更新 更多