lost baby
lost baby

Reputation: 3268

What is wrong with this rewrite rule in a .htaccess file?

RewriteEngine On

RewriteCond search.php
RewriteCond {$QUERYSTRING} category=restauran(.*)city=Montrea(.*)$ 
RewriteRule search.php.* http://www.mynewsite.fak/$1 [P]

The idea is to redirect any calls to search.php with category=restaurant and city=Montreal in the querystring to http://www.mynewsite.fak with path intact so for example:

myoldsite.fak/folderA/folderB/search.php?blah=blah...

goes to

mynewsite.fak/folderA/folderB/search.php?blah=blah...

Upvotes: 1

Views: 92

Answers (2)

lost baby
lost baby

Reputation: 3268

this seems to be working for me:

RewriteCond %{QUERY_STRING} category=restaurant(.*)&city=Montreal

RewriteRule ^search.php$ http://www.fakesite/data/testing/search.php [P,QSA]

Thanks newfurniturey.

Upvotes: 0

newfurniturey
newfurniturey

Reputation: 38456

Three things that I notice.

The first is, you mispelled the query-string variable in your example. It should be {$QUERY_STRING}. This may be enough to fix the issue, but there are a few other suggestions as well.

The second is that your RewriteCond for the query-string is doing a greedy-match for each value. Try updating to match content that can specifically go in the fields, like \w+ for instance.

The third is the RewriteRule itself. You can drop the .* from the current page name, and change the rule to something more suitable, such as [R=301, L]. This will cause the rule to "redirect" with an HTTP 301 (permanent redirect) header, and the L states it's the last rule to apply.

Altogether, this should (potentially) work:

RewriteEngine On

RewriteCond {$QUERY_STRING} category=(\w+)&city=(\w+)$
RewriteRule ^search.php$ http://www.mynewsite.fak/$1 [R=301, L]

Upvotes: 1

Related Questions