简体   繁体   English

Apache mod_rewrite/mod_redirect:将带有查询参数的 URL 转换为对 SEO 友好的 URL。 一些查询参数被忽略

[英]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:最好用一个例子来解释:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM