htaccess的重定向存在一个特定的URL变量 [英] htaccess redirect for existence of a specific URL variable

查看:339
本文介绍了htaccess的重定向存在一个特定的URL变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读辛苦了好几个小时,但仍无法找到我所需要的!我希望有人在这里可以提供帮助。

I've been reading hard for a good few hours and still can't find what I need! Hopefully someone here can help.

我想实现的是重定向特定的URL与另一页特定的变量,而不是当有其他URL变量present。

What I want to achieve is to redirect a specific URL with a specific variable to another page, but not when there are other URL variables present.

如:

  • 的index.php选项= com_user - 这需要重定向到index.php
  • 的index.php选项= com_user和放大器;查看=登录 - 这不能被重定向
  • 的index.php选项= com_user和放大器;查看=登录和放大器;富=酒吧 - 这不能被重定向

我发现很多的例子,测试一个给定的变量的存在,但我想,以测试该变量和测试,没有其他的变量存在。

I've found lots of examples that test for the existence of a given variable, but I want to test for that variable and test that no other variables exist.

谁能帮助?

在此先感谢。

推荐答案

可以说,如果你只是做一个内部重定向,你的脚本可以忽略该参数如果其他参数都没有present。但是,这是不是你问什么,让我们看看如何可以用的mod_rewrite 来完成。

Arguably, if you'll only be doing an internal redirection, your script can just ignore that parameter if other parameters are not present. But, that wasn't what you asked, so let's see how this can be done with mod_rewrite.

如果我们只关心是否有什么的其他查询字符串,我们可以简单地检查选项= com_user 是唯一有

If we just care about if there's anything else in the query string, we can simply check if option=com_user is the only thing there:

RewriteEngine On

RewriteCond %{QUERY_STRING} =option=com_user [NC]
RewriteRule index\.php index.php?

不过,这仍然让 /index.php?option=com_user&complete=nonsense 漏网之鱼,所以如果我们想一点更严格,我们可以做这样的事情:

However, this would still allow /index.php?option=com_user&complete=nonsense to slip through, so if we wanted to be a little more restrictive, we could do something like this:

RewriteEngine On

# Check if the query string contains option=com_user
RewriteCond %{QUERY_STRING} (^|&)option=com_user(&|$)
# Check that all of these other parameters were not provided
RewriteCond %{QUERY_STRING} !(^|&)view=
RewriteCond %{QUERY_STRING} !(^|&)foo=
RewriteRule index\.php index.php?

这篇关于htaccess的重定向存在一个特定的URL变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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