[英]Apache mod_rewrite/mod_redirect: convert URL with query parameters into an SEO-friendly URL. some query parameters are ignored
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如果我访问上述 3 个示例 URL 中的任何一个,我想重定向到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:您可以使用 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
.第一个条件捕获p1
的值并将其传递给同样捕获p2
值的第二个条件。 These are then used in the substitution string as %1
and %2
backreferences respectively.然后在替换字符串中将它们分别用作%1
和%2
反向引用。
The URL parameters can occur in any order. URL 参数可以按任何顺序出现。
@
(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
. @
(在第二个条件中)只是一个不会出现在v1
的任意字符,因此在搜索p2
时用作v1
和查询字符串之间的分隔符。
All other URL parameters (ie. p3
.. pN
) are ignored (and discarded).所有其他 URL 参数(即p3
.. pN
)都将被忽略(并丢弃)。
Parameter p1
must exist with a non-empty value (otherwise the resulting redirect will be ambiguous).参数p1
必须存在一个非空值(否则结果重定向将不明确)。 p2
must also exist, but the value can be empty . p2
也必须存在,但值可以为空。 eg.例如。 /my-page?p2=&p1=v1
would still be succesfully redirected to /my-page/v1/
. /my-page?p2=&p1=v1
仍然会成功重定向到/my-page/v1/
。
The QSD
flag discards the original query string from the request. QSD
标志会丢弃请求中的原始查询字符串。
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.根据使用这些指令的位置(例如目录或服务器上下文)以及最终路由 URL 的方式,您可能需要添加初始条件以确保仅处理直接请求(而不是重写的请求)。
For example, add the following as the first condition if required:例如,如果需要,添加以下作为第一个条件:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.