.htaccess use parameter from request_uri and query_string

我是研究僧i 提交于 2021-01-27 11:50:44

问题


I try to get call a page with this structure:

/page/my-selection.html?p=1
/page/my-selection2.html?p=3
/page/my-selection3.html?p=6

where my-selection, my-selection2, ... is a database key and p the pagination. I want to redirect this on one single page, which does all the magic, but how i can use mod_rewrite to use the variables from both RewriteCond's?

I tried it this way, bit it doesn't work:

RewriteCond %{REQUEST_URI} page/(.*)\.html [NC]
RewriteCond %{QUERY_STRING} ^p=(.*)
RewriteRule page/(.*)\.html$ /rewrites/page.php?selection=$1&pagination=$2 [NC]

Examples:

  • /page/my-selection.html?p=1 should redirect to /rewrites/page.php?selection=my-selection&pagination=1
  • /page/my-selection2.html?p=3 should redirect to /rewrites/page.php?selection=my-selection2&pagination=3
  • /page/my-selection3.html?p=6 should redirect to
    /rewrites/page.php?selection=my-selection3&pagination=6

Any Ideas?!


回答1:


Try with:

RewriteCond %{QUERY_STRING} ^p=(.*)
RewriteRule ^page/(.*)\.html$ /rewrites/page.php?selection=$1&pagination=%1 [NC,L]

RewriteRule backreferences: These are backreferences of the form $N (0 <= N <= 9), which provide access to the grouped parts (in parentheses) of the pattern, from the RewriteRule which is subject to the current set of RewriteCond conditions..
RewriteCond backreferences: These are backreferences of the form %N (1 <= N <= 9), which provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions. http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond



来源:https://stackoverflow.com/questions/36589632/htaccess-use-parameter-from-request-uri-and-query-string

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