htaccess 重定向是否存在特定 URL 变量

发布于 2024-09-16 10:10:46 字数 398 浏览 3 评论 0原文

我已经努力阅读了好几个小时了,但仍然找不到我需要的东西!希望这里有人可以提供帮助。

我想要实现的是将具有特定变量的特定 URL 重定向到另一个页面,但当存在其他 URL 变量时则不会。

例如。

  • index.php?option=com_user - 需要重定向到index.php
  • index.php?option=com_user&view=login - 不得重定向
  • index.php?option=com_user&view=login&foo=bar -这不能被重定向

我已经找到了很多测试给定变量是否存在的示例,但我想测试该变量并测试是否不存在其他变量。

有人可以帮忙吗?

提前致谢。

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

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.

eg.

  • index.php?option=com_user - this needs to be redirected to index.php
  • index.php?option=com_user&view=login - this must not be redirected
  • index.php?option=com_user&view=login&foo=bar - this must not be redirected

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.

Can anyone help?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡笑忘祈一世凡恋 2024-09-23 10:10:46

可以说,如果您只进行内部重定向,并且其他参数不存在,您的脚本可以忽略该参数。但是,这不是您所要求的,所以让我们看看如何使用 mod_rewrite 来完成此操作。

如果我们只关心查询字符串中是否有其他内容,我们可以简单地检查 option=com_user 是否是唯一的内容:

RewriteEngine On

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

但是,这仍然允许 /index.php?option=com_user&complete=nonsense 来溜过去,所以如果我们想要更严格一些,我们可以这样做:

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?

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.

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?

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?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文