0

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]
haukish
  • 3
  • 3
  • ok, I guess I found the rule: `RewriteCond %{QUERY_STRING} ^id=5$ [or]` `RewriteCond %{QUERY_STRING} ^id=5&` `RewriteCond %{QUERY_STRING} !name` `RewriteRule ^site\.php$ http://mysite.com/folder/5_some_txt.html? [L,R=301]` – haukish Aug 21 '12 at 07:37

1 Answers1

1

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.

Community
  • 1
  • 1
TerryE
  • 10,724
  • 5
  • 26
  • 48
  • Thanks for your tip, I wonder if I can add somehow more parameters to exclude on the second line. I realised that I need two more there. I copied the line. It works but probably there is a shorter solution. Can you advise something? – haukish Aug 22 '12 at 08:17
  • `RewriteCond %{QUERY_STRING} !(\bname\b|\bpar2\b|\bpar3\b)` Is it correct? – haukish Aug 22 '12 at 08:40
  • See addition to my ans. BTW if this is what you want then perhaps tick on the Ans :-) – TerryE Aug 22 '12 at 09:07