Forum Moderators: phranque

Message Too Old, No Replies

Redirect single query strings

Redirect invalid query strings

         

cheaperholidays

7:06 pm on Nov 1, 2006 (gmt 0)

10+ Year Member



Hello

We need to redirect invalid query strings on individual urls
for eg www.foo.com/weather-corfu.htm?id=52 is invalid and we need to redirect to www.foo.com/weather-corfu.htm

The code below causes a 500 error any one have any ideas

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{query_string} ^id=52
RewriteRule ^corfu-weather-forecast\.htm$ [foo.com...] [R=301,L]

Thanks in Advance

Pete

BarryStCyr

7:21 pm on Nov 1, 2006 (gmt 0)

10+ Year Member



This is creating a loop.

You have a question mark at the end of the redirected path.

What about just taking the query string and processing it in the cgi. If it is not valid, redirect it to something that is valid or provide the output you want.

What are you processing the corfu-weather-forecast.htm page with? Is it php?

If it is process the query string with php since you are going to the same page anyway.

Hope this helps
Barry

jdMorgan

7:31 pm on Nov 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The question mark at the end of the substitution URL is required to clear the existing query string. This will not cause a loop. However, I do not recommend playing fast and loose with capitalization in either Apache server variables or in directives.

Also, that code will only work if "id=52" is at the beginning of the query. If you're not sure, then it's best to handle all possibilities. Therefore, I'd recommend:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{[b]QUERY_STRING[/b]} &?id=52&?
RewriteRule ^corfu-weather-forecast\.htm$ http://www.foo.com/corfu-weather-forecast.htm? [R=301,L]

If nothing changes, then examine your server error log. It will often tell you exactly what is wrong.

Jim

cheaperholidays

8:32 am on Nov 2, 2006 (gmt 0)

10+ Year Member



Thanks Jim that worked a treat, can you advise if we have to copy the entire code for every instance

eg Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} &?id=52&?
RewriteRule ^corfu-weather-forecast\.htm$ [foo.com...] [R=301,L]

or can we just use the bottom bit

RewriteCond %{QUERY_STRING} &?id=52&?
RewriteRule ^corfu-weather-forecast\.htm$ [foo.com...] [R=301,L]

Best wishes

Pete

jdMorgan

6:42 pm on Nov 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The Options and RewriteEngine directive should appear only once in the file, at the top of you mod_rewrite code.

Also, you have only given one example. Therefore, although it is likely that you cna use a single rule to handle more than one redirect, I can only guess at what other redirects you might be wanting to do.

For example, if multiple query strings on the same page need to be removed, then you might use:


RewriteCond %{QUERY_STRING} &?id=52&? [OR]
RewriteCond %{QUERY_STRING} &?loc=(17¦42)&? [OR]
RewriteCond %{QUERY_STRING} &?pkg=[1¦2¦33)&?
RewriteRule ^corfu-weather-forecast\.htm$ http://www.foo.com/corfu-weather-forecast.htm? [R=301,L]

That redirects to remove the query string if the requested page is /corfu-weather-forecast\.htm, and if the query string contains "id=52", "loc=17", "loc=42", "pkg=1", "pkg=2", or "pkg=33".

Or if the same query string on multiple pages needs to be redirected:


RewriteCond %{QUERY_STRING} &?id=52&?
RewriteRule ^corfu-(weather-forecast¦lodging¦tours)\.htm$ http://www.foo.com/corfu-$1.htm? [R=301,L]

which redirects to the requested page but removes the query string if the requested page is /corfu-weather-forecast.htm, /corfu-lodging.htm, or /corfu-tours.htm, and the query string contains "id=52".

There are many, may ways to handle multiple URL and query variations with a singe rule or a small number of rules. The above code snippets are just examples, since I don't know the scope of the variable fields in your URLs and query strings because only one example was provided.

Replace all broken pipe "¦" characters in the examples above with solid pipe characters from your keyboard before attempting to use the code; Posting on this forum modifies the pipe characters, and changes their meaning.

Jim