checkenginelight
checkenginelight

Reputation: 1146

Mod Rewrite General Rule for ALL Pagination?

Could something like this be done? No matter what page or directory page=$1 appears under it'll be rewritten/redirected to /$1

For example:

file.php/1 would be file.php?page=1

dir/file/2 would be dir/file?page=2

dir/file.php?name=something/3 would be dir/file.php?name=something&page=3

Here's what I have so far:

RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteCond %{QUERY_STRING} ^&page=([0-9-]+)/?$ 
RewriteRule ^(.+)\.php$ $1/%2 [R=301,L]
RewriteRule ^(.*)$ $1.php/$2

Upvotes: 0

Views: 942

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

To cover the URI paths that end with /123 we can use this rule:

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

So:

  • file.php/1 would be file.php?page=1
  • dir/file/2 would be dir/file?page=2
  • Note that this will also cover: dir/file/3?foo=bar would be dir/file?page=3&foo=bar

To cover the /123 that gets appended at the end of the actual query string, we can use this rule:

RewriteCond %{QUERY_STRING} (.*)/([0-9]+)$
RewriteRule ^(.+)$ /$1?%1&page=%2 [L]

So:

  • dir/file.php?name=something/3 would be dir/file.php?name=something&page=3

Upvotes: 1

Related Questions