友好的URL重写规则与分页

huangapple go评论61阅读模式
英文:

rewriterule friendly url with pagination

问题

我需要更改我的RewriteRule。
我有一个友好的重写URL:

RewriteRule ^homepage/([^/]+)$ /home.php?path=$1 [L]

它处理形式为/homepage/gite-di-1-giorno的URL。

这个方法有效。但同一页还需要处理分页,例如:/homepage/gite-di-1-giorno?page=2

我尝试了这个:

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

$_GET["page"]没有设置。

英文:

I need to change my rewriterule.
I have a rewrite friendly url:

RewriteRule ^homepage/([^/]+)$ /home.php?path=$1 [L]

Which handles URLs of the form /homepage/gite-di-1-giorno.

This works. But same page has to handle pagination as well, for example: /homepage/gite-di-1-giorno?page=2

I tried this:

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

but $_GET["page"] is not set.

答案1

得分: 0

只需将 QSA(Query String Append)标志添加到您的原始规则中即可。例如:

RewriteRule ^homepage/([^/]+)$ /home.php?path=$1 [QSA,L]

QSA 标志会将原始请求的查询字符串(如果有)与您在替代字符串中设置的查询字符串合并。因此,形式为 /homepage/gite-di-1-giorno?page=2 的请求会在内部重写为 /home.php?page=gite-di-1-giorno&page=2

参考链接:

英文:

It would seem you just need to add the QSA (Query String Append) flag to your original rule. For example:

RewriteRule ^homepage/([^/]+)$ /home.php?path=$1 [QSA,L]

The QSA flag appends (merges) the query string on the original request (if any) with the query string you are setting in the substitution string. So, a request of the form /homepage/gite-di-1-giorno?page=2 is internally rewritten to /home.php?page=gite-di-1-giorno&page=2.

Reference:

huangapple
  • 本文由 发表于 2023年3月7日 00:25:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定