mod rewrite in htaccess with paging system

倾然丶 夕夏残阳落幕 提交于 2020-01-15 07:26:29

问题


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]

回答1:


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]


来源:https://stackoverflow.com/questions/15106056/mod-rewrite-in-htaccess-with-paging-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!