1

I'm trying to find a way to stop tag redirections to the homepage so that https://example.com/?something stops redirecting to https://example.com.

Similarly, any URL which is followed by a question mark (?) I want to be redirected to the 404 page instead of the site homepage.

  • 2
    "https://example.com/?something stops redirecting to https://example.com" - Are you actually seeing an external redirect, as you state? In which case you need to find the source of that redirect. Or is the homepage simply served at the URL https://example.com/?something - no redirection - which would be expected/default behaviour? – MrWhite Jul 28 at 22:56
  • And what do you want to happen when receiving a request for https://example.com/?something? A 404, as mentioned in the 2nd part of the question? – MrWhite Jul 29 at 21:27
  • Hello :) The homepage is served without any issues. I want to stop the ?something to be redirected to the homepage. This is a default action probably from the server side – makmour Jul 31 at 12:34
1

any URL which is followed by a question mark (?) I want to be redirected to the 404 page

Using mod_rewrite at the top of your .htaccess file you can do something like the following to serve a 404 for any request that contains a query string (the part after the first ?).

RewriteEngine On

RewriteCond %{QUERY_STRING} .
RewriteRule ^ - [R=404]

The single dot in the CondPattern (2nd argument to the RewriteCond directive) matches any character in the query string. If no match, ie. no query string, then the 404 is not served.

Note that this is not an external "redirect". The 404 error document is served via an internal subrequest.

https://example.com/?something stops redirecting to https://example.com

I'm assuming there is no actual "redirect" here and the home page is simply being served at that URL. https://example.com/?something is, after all, the URL for the home page (an empty URL-path), just with an additional query string.

However, if you are seeing an external redirect to https://example.com/, then this is something you need to resolve first - where is the "redirect" coming from?


UPDATE: This is about a hacked site where Google has chaced for months pages with spam content.

To get these "spam" URLs removed as quick as possible from Google then consider sending a "410 Gone" instead of a standard 404. A 410 is a stronger signal for Google that the page is never coming back and should drop the result quicker.

To do this, simply change the R=404 to G (short for R=410) in the above RewriteRule directive. For example:

RewriteCond %{QUERY_STRING} .
RewriteRule ^ - [G]

To serve a 410 Gone for any request that contains a query string (everything after the first ?).

  • Thanks a lot for your reply. This is about a hacked site where Google has chaced for months pages with spam content. Those pages URLs are similar to example.com?q=hfgf or example.com/?hydftgd Even though the hack has been removed Google still has those spam pages cached because on their part they don't serve a 404 since they redirect to the homepage as 301 or 302. – makmour Jul 31 at 12:38
  • If these URLs are the result of a hack and to get them removed from Google as-soon-as then consider sending a 410 instead of a 404. I've updated my answer. – MrWhite Jul 31 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.