【问题标题】:Check to see if a key exists in a query string - htaccess检查查询字符串中是否存在键 - htaccess
【发布时间】:2011-01-17 14:03:22
【问题描述】:

我的网址有如下查询字符串:

?v=11&icid=someid&near=far
OR
?icid=some&v=11&near=far
OR
?icid=some&near=far&v=11
OR
?v=11
OR
There may be more but "v" exists

在htaccess中,请问我该怎么做?

如果键“v”存在 和 该值不是 [1-11] 重定向到我的网站

到目前为止,我得到了以下内容:

示例网址:http://www.mysite.com/2010/01/17/breaking-news/? (以上 QS 之一)

RewriteCond %{QUERY_STRING} v
RewriteCond %{QUERY_STRING} v=([^1-11]+)
RewriteRule ([1000-9999]+)/([^/]+)/([^/]+)/([^/]+)/?([0-99]+)?$ http:// % {HTTP_HOST}/? [右,左]

非常感谢您对此的任何帮助。

谢谢,
L

【问题讨论】:

  • [^1-11] 肯定是错的。

标签: .htaccess


【解决方案1】:
RewriteCond %{QUERY_STRING} v=([^&]+)
RewriteCond %1 !^(?:\d|11)$
RewiteRule ... stuff

【讨论】:

    【解决方案2】:

    这是我的尝试:

    #grab the v parameter, allowing it to appear anywhere within the query string,
    #without matching part of another parameter name such as "gov"
    RewriteCond %{QUERY_STRING} (?:^|&)v=([^&]*)(?:$|&)
    
    #check if it's NOT set to a digit between 1-11
    RewriteCond %1 !^(?:[1-9]|1[01]?)$
    
    #Begin matching anywhere within the requested URL, since there's no leading caret.
    #If this should match the beginning, and not somewhere within the middle,
    #then add a leading caret.
    #The logic is as follows:
    #Check if we have a number between 1000 and 9999 followed by a slash;
    #then three slash delimited chunks with slashes in between;
    #then an optional chunk which, if matched, must begin with a slash, then 
    #a number between 0 and 99 (including a leading zero);
    #followed by an optional tailing slash;
    #followed by the end of the string.
    RewriteRule ([1-9]\d{3})/([^/]+)/([^/]+)/([^/]+)(?:/(\d\d?))?/?$ http://%{HTTP_HOST}/? [R,L]
    
    #more rules can go here if we didn't redirect
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 2020-04-10
      相关资源
      最近更新 更多