2

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.

2
  • 1) What have your tried so far? 2) Why bother. Commented Sep 16, 2015 at 13:28
  • I've added 1 and 2 to the question above Commented Sep 16, 2015 at 13:55

1 Answer 1

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();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
Tad late but here is solution hopefully this helps htaccess 301 redirect - Remove query string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.