本文介绍了基于参数值的HTAccess重写,忽略其他参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另一个HTAccess重写问题,对不起。...

another HTAccess rewrite issue, sorry....

我正在尝试重写所有具有menuid = 28421,menuidid = 27384和menuid = 73841的URL。问题在于menuid是较长查询字符串的一部分,但查询字符串中并非所有参数都存在。我只想根据menuid的值重写,而忽略其他参数。

I'm trying to rewrite all URL's that have menuid=28421, menuid=27384 and menuid=73841. The issue is that the menuid is part of a longer query string, but not all parameters are present in the query string. I want to rewrite only based on the value of menuid, and ignore the other parameters.

http://www.domain.com/shop.php?menuid=28421&menuref=Ja&menutitle=New+Products&limit=5&page=8
http://www.domain.com/shop.php?menuid=27384&menuref=Ic&menutitle=Old+Products&page=3
http://www.domain.com/shop.php?menuid=73841&menuref=Hd&limit=10&page=14

重定向结果:

http://www.domain.com/new.html
http://www.domain.com/old.html
http://www.domain.com/sale.html

在以下进行了尝试,但没有骰子:

Tried this below, but no dice:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^menuid=28421$ [NC]
RewriteRule ^shop\.php$ http://www.domain.com/new.html? [R=301,NE,NC,L]

我会根据我的产品的menuid自动生成wegpages类别:

I automatically generate wegpages based on menuid for my product categories:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^menuid=28421&page=1$ [NC]
RewriteRule ^shop\.php$ http://www.domain.com/new.html? [R=301,NE,NC,L]

@anubhava建议添加:

Suggested addition by @anubhava:

## 301 Redirects of Individual Menus, Ignoring the Limit Parameter etc...
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)menuid=(?:28421|12356)(?:&|$) [NC]
RewriteRule ^shop\.php$ http://www.domain.com/new.html? [R=301,NE,NC,L]

谢谢您的任何建议,

标记。

推荐答案

在此,您需要使用更好的正则表达式来匹配QUERY_STRING

You need to use a better regex for matching QUERY_STRING in this case.

使用以下规则:

RewriteEngine On

RewriteCond %{QUERY_STRING} (?:^|&)(menuid=[^&]+).+ [NC]
RewriteRule ^shop\.php$ %{REQUEST_URI}?%1 [R=302,NE,NC,L]

menuidid = 28421 可以出现在QUERY_STRING中的任意位置。

menuidid=28421 can appear in QUERY_STRING at any position.

这篇关于基于参数值的HTAccess重写,忽略其他参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 22:11