Search code examples
.htaccessmod-rewriteurl-rewritinghttp-status-code-301

301 redirect get pagination


I'm trying to 301 redirect a paginated blog list from an old site onto a new url.

I think I'm getting pretty close with the RewriteRule but I'm not quite there yet, this is what I have:

RewriteCond %{QUERY_STRING} ^page=
RewriteRule ^(blog)?$ http://www.newdomain.com/news/page/$1? [R=301,L]

Using this rule if I go to

http://www.olddomain.com/blog?page=1

I currently get redirected to

http://www.newdomain.com/news/page/blog

I would like to be sent to

http://www.newdomain.com/news/page/1

I'm sure its just something small and simple that I'm missing.

Edit

Expanding on the solution below, I've added tags/category support to the rewrite rule using $1.

RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
RewriteRule ^blog/tag/([^/\.]+)?$ http://www.newdomain.com/news/tag/$1/page/%1? [R=301,L,NC]

Solution

  • Few minor mistakes in your code.

    1. You need to capture page parameter's value from query string first
    2. Then use that capture value using % instead of $1
    3. No need to capture blog since you don't need it.

    Change your code with:

    RewriteCond %{QUERY_STRING} ^page=([^&]+) [NC]
    RewriteRule ^blog/?$ http://www.newdomain.com/news/page/%1? [R=301,L,NC]