1

We are using WordPress and what I would like to do is to redirect any requests to our main blog page that feature a query string, to the main blog page without a query string. The only exception being a search query.

Therefore:

/blog/?gibberish should redirect to /blog/

/blog/?gibberish=gibberish should redirect to /blog/

/blog/?s=cats should be processed as normal and not redirect

For simplicity here is the default WordPress redirect rules

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
# END WordPress

Here is what I have tried so far

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
RewriteCond %{QUERY_STRING} .
RewriteRule ^$ http://domain.com/ [R=301,L]
# END WordPress

However this comes up with an error in the browser saying the page isn't redirecting properly.

The reason we want to do this is because we're using a caching plugin, but it doesn't cache requests with a query string. At the moment we have lots of malicious requests using nonsense query strings which is bypassing the caching and causing a load on the server.

MrCarrot
  • 2,546
  • 1
  • 23
  • 29

1 Answers1

1

You could do this in functions.php as well, executing a function on init to check for bad queries and redirecting as needed.

add_action('init', 'redirectQuery', 0);
function redirectQuery(){
    if( isset($_GET['gibberish']) == true ){
            wp_redirect(home_url(), 301);
            die();
    }
}
santi6291
  • 154
  • 2
  • 9
  • The problem there is this will still make various database connections via WordPress. The idea of both the redirect and the subsequent caching is that it avoids the call to WordPress altogether – MrCarrot Sep 16 '15 at 14:33
  • Tad late but here is solution hopefully this helps [htaccess 301 redirect - Remove query string](http://stackoverflow.com/a/21118575/3276081) – santi6291 Nov 07 '15 at 18:45