开发者

htaccess redirect for existence of a specific URL variable

开发者 https://www.devze.com 2023-01-13 10:05 出处:网络
I\'ve been reading hard for a good few hours and still can\'t find what I need!Hopefully someone here can help.

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.


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?
0

精彩评论

暂无评论...
验证码 换一张
取 消