Using Apache .htaccess file to remove Query Strings

July 31st, 2012 admin Location: Raleigh, NC, Topic: Web Design | Digital Media

So today’s post is a little bit more technical than usual. But I’m sure that some of our technical readers will get a lot of value out of it.
In the world of SEO everybody is trying to move ahead, gain an advantage. Because of this fact, sometimes some webmasters are willing to take positions that are unethical. Sometimes they manipulate their own websites to get an advantage. However, sometimes they manipulate other people’s websites to take advantage as well.

One of this manipulation techniques is done through the query strings of your website. Let me explain:

If your website uses a content management system, it relies on query strings to search for the appropriate pages to serve. And sometimes because of the configuration of your server this can be exploited.

In this case the exploitation happens in the following manner:
1. The spammer creates a link to a non-existent page on your website of the form: http://www.yourwebsite.com/?referrer=www.thespammerssite.com
2. The spammer hopes that your CMS is configured in such a way that when Google visits this URL it takes the spider to the spammers website with a 301 redirect.
3. The benefit being that you are passing link juice or link value to the spammers website.

How do we prevent this from happening

Well, if you are you are using an IIS server, the solution might be a little bit more complex to find. However I am going to explain the solution in an Apache server. The techniques explained here shouldn’t be used by beginners, anything that you do with your htaccess file can render your website unavailable. Also I always recommend making a backup of all the available files before starting.

First open your .htaccess file in your home directory. Then, add the following lines to it:


RewriteCond %{QUERY_STRING} referrer=
RewriteRule (.*) http://www.yoursite.com/$1? [R=301,L]

The first line is the qualification line. And basically what he means is if the query string of the request matches the keyword “referrer”, Then go ahead and apply the Rewrite Rule.

The second line is of course the description of the rule. It starts with the keyword RewriteRule. Then it has a space. The first parameter is two parentheses with the dot and an asterisk inside. The dog in regular expressions means match zero or any character. The * means match the previous match as many times as needed (in this case the dot). In other words what that means is match any characters as needed. In this particular example what that’s going to do is to match completely the request.

After that we have another space. And the second parameter is the results of your rewrite. In this case HTTP://www.yoursite.com/$1? – The $1 means the matching that we did in the first parameter – because we encapsulated it with parentheses. The? Eliminates any query strings.

The final parameter is the set of flags for this rewrite rule. The first flag means rewrite with a code of 301 which means the permanent rewrite. The second flag is an “L”, which means this is the last rule that we want to apply.

And that’s it.

Of course in this case we are only approaching that referrer query string, so make sure to keep an eye on your log files to always identify what are the spammers trying to use as your query strings.

About the Author

Alex Centeno MBA., is an international creative director and digital media strategist. Currently residing minutes away from the Research Triangle Park (RTP) - North Carolina, and with over 10 years of interactive marketing training and experience, Alex leads Merkados' international clients to maximizing their online business strategies. One of Alex's biggest strengths is his world-class capacity to effectively combine interactive marketing, digital media design and web development.

You may find further information about Alex Centeno MBA. at: and Twitter.

Sobre el Autor

Alex Centeno MBA., Es un director creativo y estratega de medios digitales reconocido internacionalmente. En la actualidad reside a tan solo minutos del Research Triangle Park (RTP) - Carolina del Norte, y con más de 10 años de entrenamiento y experiencia en marketing interactivo, Alex lidera a los clientes internacionales de Merkados™ para maximizar sus estrategias de negocios en línea. Una de las mayores fortalezas de Alex es su capacidad global de combinar de manera efectiva, mercadeo interactivo, diseño de medios digitales y desarrollo web.

Para más información sobre Alex Centeno MBA. puede visitar: y Twitter.