简体   繁体   中英

htaccess redirect query string to homepage

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM