Retain query string on rewrite .htaccess

一世执手 提交于 2020-01-04 06:00:56

问题


I currently have a simple rewrite that redirects

/photos/2

to

/photoviewer.php?photo=2

However I need to be able to allow the user to add a photo to their cart so I was thinking I need to retain the query string so that the following works.

/photos/2?action=purchase

redirects to:

/photoviewer.php?photo=2&action=purcahse

My current htaccess rule is:

RewriteRule ^photos/([a-zA-Z0-9_-]+)$ photoviewer.php?photo=$1 [L]

回答1:


Append the [QSA] flag (query string append).

RewriteRule ^photos/([a-zA-Z0-9_-]+)$ photoviewer.php?photo=$1 [L,QSA]



回答2:


In your rule, replace [L] with [L,QSA].
That will then retain the query information.

Hope this helps!




回答3:


[QSA,L] instead of just [L] should do the trick. Hope this helps!




回答4:


How about just extending the idea to pass "any" name/value pairs thru the URL to the photoviewer.php script? This is an elegant approach I've used in the past.

RewriteRule  ^photos\/([^/\.\-]+)\/([^/\.\-]+)$    /photoviewer\.php\?$1=$2    [L]
RewriteRule  ^photos\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)$    /photoviewer\.php\?$1=$2&$3=$4    [L]
RewriteRule  ^photos\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)\/([^/\.\-]+)$    /photoviewer\.php\?$1=$2&$3=$4&$5=$6    [L]

So, something like:

photos/photo/2/action/purchase

rewrites to:

photoviewer.php?photo=2&action=purchase


来源:https://stackoverflow.com/questions/8081641/retain-query-string-on-rewrite-htaccess

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