Remove ?p=n from querystring and 301 to clean URL

Posted on 2013-09-12
Medium Priority
Last Modified: 2013-10-23

Our urls used to be paginated - but we now have AJAX loader.

Our URL's indexed in Google are:

The rewrite rule needs to identify any URL's that contain "?p=n" immediately after ".html" and only on these do a 301 redirect to the same URL but without any querystring.

Question by:intangiblemedia
  • 2
LVL 58

Expert Comment

ID: 39487018
Ajax is for your visitors benefit, you still need to let Google etc index the paginated pages. Search engines wont execute ajax requests.
If you remove all links to the paginated pages then you will lose all those pages from Google etc. ergo search traffic

Author Comment

ID: 39487033
Thank you, but we are no longer using pagination - and these querystrings no longer affect the product listing, so it is actually causing duplicate content in Google's eyes.
LVL 58

Expert Comment

ID: 39487050
And how does Google get the rest of the content?
Assume you are registered with Google Webmaster Tools, you can set what params do under the Crawl index.
In your htaccess, this will remove the whole querystring!

RewriteEngine On
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^(.*)$ /$1? [R=301,L]

Open in new window

LVL 18

Accepted Solution

DropZone earned 2000 total points
ID: 39492921

Try the following.  It detects the "p=..." as the first argument in the query string (as you requested), which may be followed by any other argument which you may want to keep.

So something like "http://www.example.com/index.html?p=1&a=foo&b=bar" would turn into "http://www.example.com/index.html?a=foo&b=bar".

RewriteEngine On
RewriteCond %{QUERY_STRING} ^p=\d+(\&(.+))?$
RewriteRule ^(.*)$ /$1\?%2 [R=301,L]

Open in new window

If you dont know if the "p=..." argument is the first one, then you need to use a more complex rule, like this:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^((?!:p=)[^&]*&)*p=\d+(\&(.+))?$
RewriteRule ^(.*)$ /$1\?%1%3 [R=301,L]

Open in new window


Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Simple Linear Regression

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question