As above.
might be best explained with an example:
original URL (the order of the params is NOT consistent)
If I visit any of the 3 sample URLs above, I want to get redirected to www.myhost.com/my-page/v1/v2
I've looked/searched the net but nothing comes close to what I'm trying to do. Thank you.
You can do it like the following using mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)p1=([^&]+)
RewriteCond %1@%{QUERY_STRING} ^([^@]+)@(?:|.*&)p2=([^&]*)
RewriteRule ^/?(my-page)$ /$1/%1/%2 [QSD,R=302,L]
The first condition captures the value of p1
and passes this to the second condition that also captures the value of p2
. These are then used in the substitution string as %1
and %2
backreferences respectively.
The URL parameters can occur in any order.
@
(in the second condition) is just an arbitrary character that does not occur in v1
and so is used as a delimiter between v1
and the query string when searching for p2
.
All other URL parameters (ie. p3
.. pN
) are ignored (and discarded).
Parameter p1
must exist with a non-empty value (otherwise the resulting redirect will be ambiguous). p2
must also exist, but the value can be empty . eg. /my-page?p2=&p1=v1
would still be succesfully redirected to /my-page/v1/
.
The QSD
flag discards the original query string from the request.
Depending on where these directives are being used (eg. directory or server context) and how the URL is ultimately being routed, you may need to add an initial condition to ensure that only direct requests (as opposed to rewritten requests) are processed.
For example, add the following as the first condition if required:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.