.htaccess QUERY_STRING查找2个变量并正确重写

时间:2011-12-05 16:59:29

标签: .htaccess

我有一个运行良好的.htaccess文件。它默默地重定向到另一个网站。对于一个特定页面,我需要在查询字符串中查找一个附加参数并将其传递给站点。不知道怎么做....到目前为止这是我的文件:

    RewriteEngine On

    RewriteRule ^ajax/(.*)$ http://final.site.com/ajax/$1 [P,L]
    RewriteRule ^includes/(.*)$ http://final.site.com/includes/$1 [P,L]
    RewriteRule ^themes/(.*)$ http://final.site.com/themes/$1 [P,L]
    RewriteRule ^css/(.*)$ http://final.site.com/css/$1 [P,L]
    RewriteRule ^js/(.*)$ http://final.site.com/js/$1 [P,L]

    RewriteCond %{HTTP_HOST} ^myusername.site.com [NC]
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule (.*) http://final\.site\.com/?username=myusername&page_name=Home [P,L]

    RewriteCond %{QUERY_STRING} page_name=(.*) [NC]
    RewriteCond %1 !=get_some_kind_of_data [NC]
    RewriteRule (.*) http://final\.site\.com/?username=myusername&page_name=%1 [P,L]

所以我只想添加这个......如果我转到http://myusername.com/?page_name=All%20Art&collection_id=6,如果page_name是其他任何内容,它应该默默地调出http://final.site.com/?username=myusername&page_name=All%20Art&collection_id...and,除了上面显示的“get_some_kind_of_data”之外,将不会传递collection_id。

1 个答案:

答案 0 :(得分:0)

尝试用以下内容替换最后一条规则:

# If page_name=get_some_kind_of_data, proxy everything in the query string along with username
RewriteCond %{QUERY_STRING} (^|&)?page_name=get_some_kind_of_data($|&)? [NC]
RewriteRule (.*) http://final.site.com/?username=myusername [P,L,QSA]

# If page_name exists but it isn't get_some_kind_of_data, match collection_id and remove it
RewriteCond %{QUERY_STRING} (^|&)?page_name=([^&]+)($|&)? [NC]
RewriteCond %1 !get_some_kind_of_data [NC]
RewriteCond %{QUERY_STRING} (.*)(^|&)?collection_id=[0-9]+(.*)  [NC]
RewriteRule (.*) http://final.site.com/?username=myusername&%1%3  [P,L]

请求:

  代理http://myusername.com/?foo=bar&page_name=get_some_kind_of_data&collection_id=6

http://final.site.com/?username=myusername&foo=bar&page_name=get_some_kind_of_data&collection_id=6

  代理http://myusername.com/?foo=bar&page_name=get_something_else&collection_id=6

http://final.site.com/?username=myusername&foo=bar&page_name=get_something_else

这就是你要找的东西吗?