Forum Moderators: phranque
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
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
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]
Jim
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
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]
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]
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