1

Google has discovered a pager in the sidebar of a website that allows the user to scroll through 'tags' while on a blog entry, each pager click taking you to a new url, e.g. example.com/blog/article-title?page=1.

Since the blog has ~ 500 posts and the pager has ~ 140 pages of tags, Google has indexed a very high number of pages for my site. I don't want this for a number of reasons. I've already removed the pager on this tag block as it's completely unnecessary, but now would like to do 301 redirects on all of these ?page=X URLs in the hopes that, over time, Google will remove them from their index.

The URLs look like this:

How can I do a 301 redirect via htaccess that will send the user to http://example.com/blog/blog-article-title ?

I've tried many variations on

  • RewriteRule ^blog/(.*).?page(.*)$ /$1/blog/$2? [R=301,L]

and

  • RedirectMatch 301 ^/blog/(.*)\?page(.*)$ /blog/$1

but no luck thus far. Any help would be much appreciated.

Edit: For others that may stumble upon this, Google had completely ignored the canonical URL that is defined in the header and indexed all 30,000+ URL combinations. Doing the htaccess rewrite in the accepted answer below is properly performing a 301 on these pages. This, of course, requires that I move the pager elsewhere, but that's already been handled in my particular situation.

Charlie Schliesser
  • 7,851
  • 4
  • 46
  • 76

3 Answers3

2

Your problem is that you are trying to redirect on the URL instead of the query string. See this question: Query string redirection with htaccess

Basically, you need a RewriteCond based on the query string. It'll be two lines; ie:

RewriteCond %{QUERY_STRING} ......
RewriteRule ....
Community
  • 1
  • 1
cegfault
  • 6,442
  • 3
  • 27
  • 49
2

Without query_string option this should work:

RewriteEngine On
# one url with many params
#RewriteRule ^blog/blog-article?(.*) http://www.yourblog.com/index\.php$1 [R=301,L]
# or many url
RewriteRule ^blog/sub(.*)\?(.*) http://www.yourblog.com/onething/$1?$2 [R=301,L]

If this doesn't work you may have some other issue in your script.

Dr. Dama
  • 167
  • 2
  • 5
2
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/blog/
RewriteCond %{QUERY_STRING} page
RewriteRule (.*) /$1? [R=301,L]

This should achieve what you are trying to do.

Sameh
  • 346
  • 2
  • 9