Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I want to redirect all the paginated pages of the category pages to their respective category pages, because I don't want google to crawl the paginated pages.

If my paginated category page URL is:

http://example.com/category/cat-name/page/4/

I want to redirect to:

http://example.com/category/cat-name/

This case can be anything from different page numbers to different category names.

How can I achieve that using .htaccess?

I'm running a WordPress site and already there are several redirects active:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^[0-9]+/([^/]+/[^/]+/?)$ /$1 [L,R=301,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

EDIT

First of all, some of my .htaccess rules are based on this StackOverflow Q&A. Because I need 'em to be there.

So, after your comments, I got the point. With such a redirection in action my paginated pages won't be accessed ever, even after a hard-coded URL. Yes, that'd be a mess.

Actually, you know WordPress category pages are auto generated with the number of posts. Suppose, in "Astronomy" category I got 32 posts and in each page I'm showing 10, then there would be 4 pages (10+10+10+2). Eventually I deleted two of the posts or recategorized them to another category. So the fourth page become orphan. Google crawls and got a 404. It's a continuous process and looking for such 404 hits is a critical matter for me. So I's looking for a solution.

What can I do for such problem?

share|improve this question
3  
But could this be bad for user experience? what happens if someone clicks your paginated pages expecting to see pages 2, 3 etc? Yuo might be better implementing rel=”next” and rel=”prev” mark up: googlewebmastercentral.blogspot.com.au/2012/03/… –  Max Jul 3 at 8:03
1  
Like @Max , Im also disagree with your requirement, all of your paginated pages will redirect to main page, so user's cannot access your paginated pages anymore, No, don't redirect, your doing the wrong way, instead use rel=”next” and rel=”prev” mark up to avoid duplication issues. –  Bala Jul 3 at 10:47
1  
Did you added the first RewriteRule in the htaccess above? I based my answer assuming you did it, I should asked about it before. –  Binarysurf Jul 3 at 13:47
    
Thank you all for your expert advices, and I got my problem first. I edited my Question so that I can make myself more clear about the scope of the question. –  Mayeenul Islam Jul 4 at 9:38
    
I guess your question turned into something very different to what it was in the beginning. I'll update my post to answer the last added part of your question. But it looks like there should be two different questions. –  Binarysurf Jul 4 at 14:24

1 Answer 1

Edit 2

Based on @w3d comment and more information provided by Mayeenul:

Replace your regex:
RewriteRule ^[0-9]+/([^/]+/[^/]+/?)$ /$1 [L,R=301,NE]
with this one:

Add this new rule:

RewriteRule ^([^/]+)/([^/]+)/page/[0-9]+/?$ /$1/$2/ [R=301,L]

So your .htaccess should look like:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/([^/]+)/page/[0-9]+/?$ /$1/$2/ [R=301,L]
RewriteRule ^[0-9]+/([^/]+/[^/]+/?)$ /$1 [L,R=301,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

This is assuming your url will allways contain page as its third url segment and numbers for pages. If it wont then replace it by [^/]+, it will be better (for matching) and easier if you keep this segment just like it is right now.

You can test htaccess rules on this tool.

You can use some alternatives to letting know bots you don't want these pages been crawled: you can block them using robots.txt, another way will be adding authentication.

You should not use redirects for preventing Google indexing (you said crawling maybe you meant indexing) your pages if they are useful for your visitors. If you 301 redirectem your visitors will never see them again, they will always get your non-paginated category. But if that's precisely what you're trying to do, then redirects are the answer.

If you are concerned about duplicate content, you should include <link rel="canonical" href="http://example.com/category/cat-name/" /> on each one of your paginated category pages. For this option you should not implement redirects.

Edit 1

If you're going to add a view all page, i.e. your main category page showing all items, its recommended to use rel="canonical" pointing from paginated pages to view all page.
If you're not going to add a view all page, i.e. you're going to keep all your paginated pages and not adding a main category page with all items, then you should use rel="prev", rel="next", as @Max pointed out.

Edit 3

If Google Webmaster Tools is reporting 404 errors and they really are 404's, you could let them just like they are if that doesn't bother you, eventually Google will drop those URLs from its index. If 404 errors bothers you then implement 301 redirects if there's a new location for those URLs. You may write a PHP script that intercepts the request and redirect when necessary, I don't know where this code should be put in WordPress' code, I'm not a WP expert.

Another option is to add those nonexistent URLs to your .htaccess and redirect them one by one. As your example http://example.com/category/cat-name/page/4/ no longer exists, then you .htaccess should be something like this:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^category/cat-name/page/4/?$ /category/cat-name/ [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/page/[0-9]+/?$ /$1/$2/ [R=301,L]
RewriteRule ^[0-9]+/([^/]+/[^/]+/?)$ /$1 [L,R=301,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
share|improve this answer
    
Isn't that RewriteRule required by WordPress? –  w3d Jul 3 at 11:05
    
@w3d good question, I didn't considered it, I assumed Mayeenul added it himself. I'll check out Wordpress' documentation and will update my answer as required. Thanks for pointing it. –  Binarysurf Jul 3 at 13:41
    
@w3d In WordPress htaccess' documentation codex.wordpress.org/htaccess I found basic WP does not have rules like that. Anyway, I asked Mayeenul about it, hopefully he can confirm it. –  Binarysurf Jul 3 at 13:57
1  
@w3d You were right, that RewriteRule is required for other redirects by WordPress, I've updated my answer. –  Binarysurf Jul 4 at 13:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.