haukish
haukish

Reputation: 3

Rewrite condition for one or more variables

I need to rewrite some site. I need to rewrite the site when it has one or more of any variables e.g.:

mysite.com/site.php?id=5 or  
mysite.com/site.php?id=5&other_variable=233 or  
mysite.com/site.php?id=5&any_stuff_here(numbers-letters-spacial_chars)

I tried some htaccess rules but it doesn't work.

Actually my rule looks like :

RewriteCond %{QUERY_STRING} ^id=5$ [or]
RewriteCond %{QUERY_STRING} ^id=5$ 
RewriteRule ^site\.php$ http://mysite.com/folder/5_some_txt.html? [L,R=301]

First condition works fine for just one variable, the second one is now the same as first one(I'm trying to put something here to handle the rest of the variables in url). I think I need to change it somehow or maybe there is a better way to achive what I'm trying to do.

Can anyone help me with this?

Edit:I have also a rule like this in the same htaccess file:

RewriteRule ^site/([0-9]+)_(.*).html$ site.php?id=$1&name=$2 [L,NC,NS]

Upvotes: 0

Views: 1069

Answers (1)

TerryE
TerryE

Reputation: 10888

Just to tidy this up, you can use the \b (word break) meta character:

RewriteCond %{QUERY_STRING} ^id=5\b
RewriteCond %{QUERY_STRING} !name 
RewriteRule ^site\.php$     http://mysite.com/folder/5_some_txt.html?   [L,R=301]

I am not sure why yuo have the name exclusion. Surely you need to be tighter here? For example !\bname= would prevent redirection if the query included the name parameter.

And taking you comment example, I would do:

RewriteCond %{QUERY_STRING} !\b(name|par2|bpar3)=

This would exclude any request which includes one of the GET parameters name, par2 or par3. Get the idea? Apache uses the PCRE regexp engine internally, so you can use any PCRE tutorial or online helper to assist. I've also include a little checker that you can add to your website in Tips for debugging .htaccess rewrite rules.

Upvotes: 1

Related Questions