Neos
Neos

Reputation: 93

mod rewrite in htaccess with paging system

I have the following code which works fine

RewriteRule ^articles/([^/\.]+)/?$ articles.php?pid=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+articles\.php\?pid=([^\s&]+) [NC]
RewriteRule ^ http://www.mydomain.com/articles/%1? [R=301,L]

The problem is that I have a paging systen and I send another variable to that page for my paging system which looks like this

&pageNum_getArticles=1#1

I've already tried to do the following but gets confused with the hash I think

RewriteRule ^articles/([^/\.]+)/?$ articles.php?pid=$1&pageNum_getArticles=$2 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+articles\.php\?pid=([^\s&]+)&pageNum_getArticles=([^\s&]+) [NC]
RewriteRule ^ http://www.mydomain.com/articles/%1/%2? [R=301,L]

Upvotes: 2

Views: 804

Answers (1)

Jon Lin
Jon Lin

Reputation: 143906

Thanks for the info. So is there a solution about this or I have to change the paging system?

Yes.

You have 2 sets of rewrite rules that sort of work together. The first rule takes a nice looking URL without any query strings and internally rewrites it to a php script. The second rule takes the ugly looking php script URI and redirects the browser to use the nicer looking one. The first set is correct. But the second set uses the same rule to route to the script. You need to create another grouping for your paging, as $2 doesn't backreference to any match.

Try:

# second match here---------------v
RewriteRule ^articles/([^/.]+)/([^/.]+)/?$ articles.php?pid=$1&pageNum_getArticles=$2 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+articles\.php\?pid=([^\s&]+)&pageNum_getArticles=([^\s&]+) [NC]
RewriteRule ^ http://www.mydomain.com/articles/%1/%2? [R=301,L]

Note that if you still want to use the rules that doesn't use paging, you need to have the non-paging rules after the ones with paging.

Additionally, if for some reason your paging doesn't work without the URL fragment, and if the fragment is always going to be the same as the paging number, you can just add it to the end of the redirect. But you'll need a NE like faa suggested:

RewriteRule ^ http://www.mydomain.com/articles/%1/%2#%2? [R=301,L,NE]

Upvotes: 1

Related Questions