homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Need Help Redirecting URLs With Parameters

 5:21 pm on Mar 26, 2010 (gmt 0)


I'm hoping someone will be willing to share some knowledge with me.

Google has recently cached a number of versions of my index page with a few parameters tacked onto the end, and I would very much like to redirect them back to my index page.

They look something like this: my_website.com/?C=M%3BO=D

I'd like to redirect everything that looks like this: my_website.com/?* (where * is wild) back to my index page. I haven't a clue how to code this in .htaccess.

Any ideas?

Thanks so much,



 6:21 pm on Mar 26, 2010 (gmt 0)

Not sure for the htaccess code but you can also create a robots.txt file that won't allow in the future spiders to cache any pages that starts with "?*"


 6:34 pm on Mar 26, 2010 (gmt 0)

Hi nimonogi,

Yeah, I've already done the exclusion in robots.txt, and that will probably take care of it, but I'm being a little extra cautious and would love to fix this permanently.

Thanks! :)


 7:24 pm on Mar 26, 2010 (gmt 0)

In order for searchengines to 'see' the redirect, you're going to have to allow them to access those URLs by removing the entries from robots.txt that currently disallow access.

If none of your site uses parameters, this fixes all URL requests with parameters.

RewriteEngine On
# Redirect non-blank query string to remove query string
# and fix www at the same time for those requests
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1? [R=301,L]
# Redirect all non-canonical (non-www, appended port
# number, etc) host name requests to www version
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1? [R=301,L]


 7:25 pm on Mar 26, 2010 (gmt 0)

Wow... thanks you so much for taking the time to write this out for me.

I'll give it a shot and let you know if it works for me!



 7:30 pm on Mar 26, 2010 (gmt 0)

OK, I just tested it, and it worked, except that some of my images disappeared. I dug further and discovered that my CMS is tacking on paramaters at the end of some images when they are being used as thumbnails on my front page.



 7:48 pm on Mar 26, 2010 (gmt 0)

OK, so (.*) matches "all", so either change the (.*) pattern to match only what it needs to match OR else add another RewriteCond that specifies either what should or should not match.


 8:00 pm on Mar 26, 2010 (gmt 0)

Thanks g1smd.

I know my lack of knowledge on this is probably kind of frustrating.

How would I change this (.*) to something that would only redirect strings such as this: ?C=M%3BO=D

I'm a total moron when it comes to this type of code. I recognize that the asterisk is a wildcard but other than that I don't know how to change that to make it work.

Could you be more specific?

Thanks a bunch.


 8:21 pm on Mar 26, 2010 (gmt 0)

It would be more useful to design the pattern to stop the redirect being actioned for URL parameters associated with your image requests OR for URL paths associated with image requests.

Before coding can begin you need a very specific set of requirements to be listed and detailed. Your initial question didn't specify exactly what parameters should be redirected, so the answer I supplied was for 'all'.

Guessing at the code will mean that it redirects too many, or not enough requests. So, detail all of the formats to be redirected and all of the formats to not be redirected first.


 8:27 pm on Mar 26, 2010 (gmt 0)

Ok, if I'm understanding you, this is what the urls look like for the thumbnailed images:


(I'm using Wordpress as my CMS.)

The urls that I'm trying to redirect are the following three:



 8:58 pm on Mar 26, 2010 (gmt 0)

ok, i think I've got it fixed... someone recommended this code to me, and it seems to be working.

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/.*(php|/).*
RewriteCond %{QUERY_STRING} ^[a-z0-9] [NC]
RewriteRule (.*) http://www.my_site.com/$1? [R=301,L]

I'll test this some more to be sure all is well.

Thanks you so much for your help. I really appreciate it.


 10:57 pm on Mar 26, 2010 (gmt 0)

So you don't want to redirect if 'php' appears in the path part of the URL request or if there is at least one folder-level depth to the path part of the URL, and only if any sort of parameter is present?

I thought we were going to fully document the requirements before thinking in any way about actual code - which is why you've ended up with the most inefficient, badly coded, ambiguous, piece of code that could ever have been conceived for the job; code that has several flaws.


 11:34 pm on Mar 26, 2010 (gmt 0)

This is closer to what was initially requested, and applies only to example.com/?blah URLs

RewriteEngine on
RewriteCond %{QUERY_STRING} .
RewriteRule ^$ http://www.example.com/? [R=301,L]

This applies only to the actual URL-path and query-strings seen in Google, and is "most specific."

RewriteEngine on
RewriteCond %{QUERY_STRING} ^C=([MN]\%3BO=D|S\%3B=A)$
RewriteRule ^$ http://www.example.com/? [R=301,L]

This applies to any URLS with query strings appended which DO NOT refer to the specific area of the WP directory that is causing trouble:

RewriteEngine on
RewriteCond $1 !^wp-content/
RewriteCond %{QUERY_STRING} .
RewriteRule ^(.*)$ http://www.example.com/$1? [R=301,L]

As gismd stated, you have to be very specific about what you need to do, and beyond that --given that there are hundreds of different and valid ways of attacking a problem-- what you *want* to do.

Coding is easy. Coming up with the requirements specification is what is difficult. :)


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved