查询字符串用重定向htaccess的
在一个网站,我的工作,现在我需要建立一个重定向,它接受一个查询字符串考虑。
On a website I'm working on now I need to set up a redirection that takes a query string into consideration.
例如:
http://www.domain.com/?id=86&name=John&ref=12d34 -> http://www.domain.com/?ref=12d34
http://www.domain.com/?ref=593x56&id=935 -> http://www.domain.com/?ref=593x56
http://www.domain.com/?ref=3v77l32 -> http://www.domain.com/?ref=3v77l32
所以基本上我需要找到裁判的参数,它的值(不管它的长度),并追加只有一部分到新的URL。问题是裁判参数可以在URL中出现在任何地方。
So basically I need to find the ref parameter and it's value (whatever it's length) and append only that part to the new URL. The issue is the the ref parameter can appear anywhere within the URL.
任何帮助或指导将非常AP preciated!
Any help or guidance would be very much appreciated!
重写规则
不直接查询字符串的工作 - 你必须使用的RewriteCond
为
RewriteRule
does not work with query string directly -- you have to use RewriteCond
for that.
下面是规则 - 它会重定向(301永久重定向),其在查询字符串超过1参数和任何URL 1人是 REF
Here is the rule -- it will redirect (301 Permanent Redirect) ANY URL that has more than 1 parameter in query string and 1 of them is ref
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)ref=([^&]*)(&|$)
RewriteCond %{QUERY_STRING} !^ref=([^&]*)$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}?ref=%2 [R=301,L]
例如:
-
这将重定向
http://www.example.com/hello.php?id=86&name=John&ref=12d34888&me=yes
到相同的URL,但与REF
参数只:http://www.example.com/hello.php?ref=12d34888
。
It will redirect
http://www.example.com/hello.php?id=86&name=John&ref=12d34888&me=yes
to the same URL but withref
parameter only:http://www.example.com/hello.php?ref=12d34888
.
它会做什么,如果仅 REF
参数是present或无参数可言,如 http://www.example.com/hello.php?ref=12d34888
或 http://www.example.com/hello.php
。
It will do nothing if only ref
parameter is present or no parameter at all, e.g. http://www.example.com/hello.php?ref=12d34888
or http://www.example.com/hello.php
.
如果这样的重定向应仅适用于网站的根命中,然后更改重写规则
行这样的:
If such redirect should only work for website root hits, then change the RewriteRule
line to this:
RewriteRule ^$ http://%{HTTP_HOST}/?ref=%2 [R=301,L]
(这是如果放在.htaccess文件在网站根目录文件夹 - 如果放在服务器配置/虚拟主机方面的规则需要稍微调整)
(this is if placed in .htaccess file in website root folder -- if placed in server config / virtual host context the rule needs to be slightly tweaked).
http://www.example.com/?id=86&name=John&ref=12d34888&me=yes
- > http://www.example.com/?ref=12d34888
如果它被重定向到另一个域名,然后替换%{HTTP_HOST}
由特定领域的名称,例如:
If it has to be redirected to another domain, then replace %{HTTP_HOST}
by domain specific name, e.g:
RewriteRule ^$ http://www.exampe.com/?ref=%2 [R=301,L]
这一切都已经在发布前进行测试。