doukuangxun5382 2015-03-07 01:49
浏览 52
已采纳

htaccess将查询字符串重定向到主页

For one of my sites, google started indexing pages like:

www.domain.com/?index.php=en-id1&itemid=3711&view=21

and they indexed several of these in just a day, making the site appear to have thousands of pages.

Other than trying to clean up the site, fixing security issues, and having google try and remove those pages from index. I want to redirect these pages to the homepage itself, so it removes the query string from the URL completely.

This is a wordpress site, so I put a rewrite condition like so, above the wordpress rewrite rules in htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On 
RewriteCond %{QUERY_STRING} ^index.php*$
RewriteRule ^index\.php$ http://domain.com/%1 [R=301,L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

How can I re-direct all pages that start with /?index.php=en-id to just my homepage?

  • 写回答

1条回答 默认 最新

  • duan02468 2015-03-07 03:24
    关注

    You can try this:

    <IfModule mod_rewrite.c>
      RewriteEngine On 
      RewriteCond %{QUERY_STRING} ^index.php
      RewriteRule ^(.*)$ http://domain.com/$1? [R=301,L]
    </IfModule>
    

    The query string will only be removed if it starts with "index.php". In addition, the above snippet will redirect to whatever page you were tring to go to. If you want any request that has a query starting with "index.php" to always redirect to the homepage. You can make the following tweak:

    <IfModule mod_rewrite.c>
      RewriteEngine On 
      RewriteCond %{QUERY_STRING} ^index.php
      RewriteRule ^ http://domain.com/? [R=301,L]
    </IfModule>
    

    Additional Resource:

    htaccess 301 redirect - Remove query string (QSA)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件