# Disable / ignore / redirect URLs with query strings to reduce blog spam

**URL:** <https://www.ruby-forum.com/t/disable-ignore-redirect-urls-with-query-strings-to-reduce-blog-spam/209215>\
**Category:** NGINX\
**Created:** [July 8, 2011, 11:17am UTC](https://www.ruby-forum.com/t/disable-ignore-redirect-urls-with-query-strings-to-reduce-blog-spam/209215 "2011-07-08T11:17:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [July 8, 2011, 11:17am UTC](https://www.ruby-forum.com/t/disable-ignore-redirect-urls-with-query-strings-to-reduce-blog-spam/209215/1 "2011-07-08T11:17:37Z")

</div>

Hello all,

Is it possible to disable / ignore / redirect all URLs  
with any query string? My site doesn’t use query strings,  
and is suffering from blog spam links as described by  
these folks:

How do I remove spam from my blog’s Google Blog Search results?  
[http://www.google.com/support/forum/p/Webmasters/thread?tid=126184f38f712907&hl=en](http://www.google.com/support/forum/p/Webmasters/thread?tid=126184f38f712907&hl=en)

Google has the wrong (somewhat vulgar) URL for my site!  
[http://www.google.com/support/forum/p/Webmasters/thread?tid=1e30ee39f9e82b4d&hl=en](http://www.google.com/support/forum/p/Webmasters/thread?tid=1e30ee39f9e82b4d&hl=en)

URL/Query String Spam - Poor Man’s Referer Spam?  
[http://www.dgibson.net/blog/article.cfm/articleid=7/URLQuery-String-Spam---Poor-Mans-Referer-Spam](http://www.dgibson.net/blog/article.cfm/articleid=7/URLQuery-String-Spam---Poor-Mans-Referer-Spam)

This Apache .htaccess example \<  
[http://briancray.com/2010/03/18/htaccess-hack-remove-url-query-strings/](http://briancray.com/2010/03/18/htaccess-hack-remove-url-query-strings/)

> 

removes all query strings like so:

RewriteEngine On  
RewriteCond %{QUERY\_STRING} !=“”  
RewriteRule ^(.\*)$ /$1? [R=301,L]

Can something similar be done in nginx? Also, might it be  
better to simply return a 204 (or some other status code)  
to reduce server load?

Sincerely,

Miles

---

<div class="post-metadata">

**Author:** ![TinyApps.Org](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/779978/32.png) [@TinyApps.Org](https://www.ruby-forum.com/u/TinyApps.Org)\
**Post date:** [July 8, 2011, 12:47pm UTC](https://www.ruby-forum.com/t/disable-ignore-redirect-urls-with-query-strings-to-reduce-blog-spam/209215/2 "2011-07-08T12:47:53Z")

</div>

On Jul 8, 2011, at 13:17 , [TinyApps.Org](http://TinyApps.Org) wrote:

> Google has the wrong (somewhat vulgar) URL for my site!  
> RewriteRule ^(.\*)$ /$1? [R=301,L]
> 
> Can something similar be done in nginx? Also, might it be  
> better to simply return a 204 (or some other status code)  
> to reduce server load?

server {  
if ($args != ‘’) {  
return 301 $uri;  
# OR  
# rewrite ^ $uri permanent;  
}  
…

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![TinyApps.Org](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/779978/32.png) [@TinyApps.Org](https://www.ruby-forum.com/u/TinyApps.Org)\
**Post date:** [July 8, 2011, 3:08pm UTC](https://www.ruby-forum.com/t/disable-ignore-redirect-urls-with-query-strings-to-reduce-blog-spam/209215/3 "2011-07-08T15:08:14Z")

</div>

Hello!

On Fri, Jul 08, 2011 at 02:47:18PM +0400, Igor S. wrote:

> >

[http://www.google.com/support/forum/p/Webmasters/thread?tid=126184f38f712907&hl=en](http://www.google.com/support/forum/p/Webmasters/thread?tid=126184f38f712907&hl=en)

> > RewriteEngine On  
> > RewriteCond %{QUERY\_STRING} !=“”  
> > RewriteRule ^(.\*)$ /$1? [R=301,L]
> > 
> > Can something similar be done in nginx? Also, might it be  
> > better to simply return a 204 (or some other status code)  
> > to reduce server load?
> 
> server {  
> if ($args != ‘’) {  
> return 301 $uri;

This will not work for complex $uri which requires encoding.

> ```
> # OR
> # rewrite ^ $uri permanent;
> 
> ```

Correct rewrite would be

```
      rewrite ^(.*) $1? permanent;

```

($1 instead of $uri to make sure it will be properly escaped, and  
“?” to avoid query string append).

> ```
> }
> ...
> 
> ```

Maxim D.

---

<div class="post-metadata">

**Author:** ![TinyApps.Org](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/779978/32.png) [@TinyApps.Org](https://www.ruby-forum.com/u/TinyApps.Org)\
**Post date:** [July 8, 2011, 7:30pm UTC](https://www.ruby-forum.com/t/disable-ignore-redirect-urls-with-query-strings-to-reduce-blog-spam/209215/4 "2011-07-08T19:30:11Z")

</div>

> > > Is it possible to disable / ignore / redirect all URLs  
> > > with any query string? My site doesn’t use query strings,  
> > > and is suffering from blog spam links  
> > > …

> ```
> rewrite ^(.*) $1? permanent;
> 
> ```
> 
> ($1 instead of $uri to make sure it will be properly escaped, and  
> “?” to avoid query string append).

Thanks so much, Igor & Maxim!

I went with:

server {  
… some earlier directives  
if ($args != ‘’) {  
rewrite ^(.\*) $1? permanent;  
… more directives follow  
}

and it works like a charm. Those blog spam links  
will be gone in no time! Thank you!!

Sincerely,

Miles
