views:

33

answers:

1

Hello all,

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.

A: 

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?
Tim Stone
Perfect! Thanks for your help Tim. I actually modified it slightly so that it redirects to an alternate login form too. :)
tulip