查询字符串用重定向htaccess的 [英] Query string redirection with htaccess

查看:186
本文介绍了查询字符串用重定向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]

例如:

  1. 这将重定向 http://www.example.com/hello.php?id=86&name=John&ref​​=12d34888&me=​​yes 到相同的URL,但与 REF 参数只: http://www.example.com/hello.php?ref=12d34888

  1. It will redirect http://www.example.com/hello.php?id=86&name=John&ref=12d34888&me=yes to the same URL but with ref 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]

这一切都已经在发布前进行测试。

这篇关于查询字符串用重定向htaccess的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆