2

A client has put their website through some QA testing, and one of the items of feedback is that the home page of the site doesn't generate a 404 error when a query string is added to the home page URL, e.g. http://mywebsite.com/?dfdfdfd=dfdfdfd&dfdfdfd This only seems to affect the home page.

Is it possible to make a URL like this go to a 404 page, instead of loading the home page? If so, how?

I hoped to use the redirect tag and to redirect the home page to a 404 page if segment_1 wasn't blank, but that doesn't work as EE doesn't seem to see the query string with segment conditionals (as expected).

I'm using strict URLs and have my 404 page setup correctly. Going to http://mywebsite.com/anythingsdfdd generates a 404 as expected. So it's just the query string URLs on the home page that I'm having issues with. Thanks in advance for any help.

4

If you are using EE 2.7+ there is a new current_query_string global variable you can test against.

{if current_query_string}
    {redirect="404"}
{/if}

Or, for versions less than EE 2.7...

Install the Mo Variables Extension and enable the setting that gives you a {query_string} variable.

Then in your homepage template you can do

{if query_string}
    {redirect="404"}
{/if}
  • Thanks for this Alex. I'm using EE 2.5.5 so I've just downloaded Mo Variables and your code above worked a treat! :) – Stephen Sep 24 '13 at 2:43
  • 1
    And just to confirm, the conditional works fine with the current_query_string global variable in EE 2.7.2. – Stephen Oct 10 '13 at 1:40
  • 1
    Great! I updated my answer to reflect this. Thanks, Stephen. – Alex Kendrick Oct 10 '13 at 3:46

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.