homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

SEO friendly pagination rewrite problem

5+ Year Member

Msg#: 4385888 posted 11:15 am on Nov 11, 2011 (gmt 0)

Hi there,
I am wanting to do some basic pagination for my articles. I have nearly 600 of them but need to do this in an SEO friendly way.

So far I have managed to get the results paginated - that wasn't the problem really.

the code is fairly standard

The pages are outputted as "/php-pagination/?page=9", but for seo purposed i would like them to be outputted as "/php-pagination/9".

However I have a mod rewrite in my .htaccess to rewrite the querystring but nothing I do works. I dont understand why this doesnt work. It is the first entry in the .htaccess file after the nonwww-www redirect.

RewriteEngine on
RewriteRule ^/php-pagination/([^/]+)$ php-pagination/?page=$1

I have tried many things I have found doing searches and I am ready to tear my hair out! Someone please help! This is a custom designed php site, I do not use a CMS.

[edited by: jatar_k at 1:37 pm (utc) on Nov 11, 2011]
[edit reason] code not needed for question [/edit]



WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

Msg#: 4385888 posted 9:11 pm on Nov 11, 2011 (gmt 0)

Here is the boilerplate on query strings. Ignore the parts you already know.
Query Strings

The Query String, also known as a Parameter, is the part of an url after the question mark. Question = query.

By default, rewrites simply ignore the query string. That is, mod_rewrite stashes the query in a safe place, does its stuff to the part before the question mark, and then reappends the original query.

Changing a Query

#1 To delete a query, add a ? to the end of your rewrite target.
#2 To replace a query—or create a new one—add ?blahblah to the rewrite target. The blahblah can be either literal text, or stuff you captured earlier. (#1 and #2 are really the same thing: you're just replacing the query with either something or nothing.)
#3 To add to an existing query, again put ?blahblah at the end of the target, but also add [QSA] to your flags (the bracketed items at the end of the Rule). It stands for "Query String Append", meaning that the blahblah is to be added to the existing query—if any—instead of replacing it.

Getting the Query

You only need to retrieve the original query if
#1 you want the rewrite to behave differently depending on what the query was
#2 you need to change or delete the query

Add a Condition that says

RewriteCond %{QUERY_STRING} blahblah

using your ordinary Regular Expressions, anchors and ! as needed.

To test whether there was a query at all

RewriteCond %{QUERY_STRING} .

which simply means "If the query contains at least one character of any kind".

If you need to capture any of the query, use parentheses as usual. In the rewrite target, the captures will be %1, %2 etc instead of $1, $2 etc, because they are coming from a Condition instead of the Rule. Each set is separately numbered, so the first capture from the Rule will still be $1.


5+ Year Member

Msg#: 4385888 posted 10:37 am on Nov 12, 2011 (gmt 0)


I sorted out the pagination converting the querystring into a path. My mod rewrite was wrong.

The following code works

RewriteRule ^news-archive/([0-9]+) /php-pagination/index.php?page=$1 [L]

As far as I understand it tells the server that any path that is like /news-archive/25 should be handled as /php-pagination/index.php?page=25, and the user would never see the querystring.

So far so good, but the pages are still available using the querystring which could lead to a duplicate content penalty.

Is there a mod rewrite to force the querystring urls to be redirected to paths?


5+ Year Member

Msg#: 4385888 posted 12:17 pm on Nov 12, 2011 (gmt 0)

got it working:

RewriteCond %{THE_REQUEST} page=([^&]+)(&[^\ ]+)?\ HTTP/
RewriteRule . http://www.domain.com/news-archive/%1? [R=301,L]
RewriteRule ^([^/_.]+)$ /php-pagination/index.php?page=$1? [L]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved