2

I have an Apache server based Wordpress site that rewrites URLs to be "pretty".

When I include a query parameter in the url the site breaks.

So:

http://mysite.com

works fine but:

http://mysite.com/?anything

Does not... loading the page contents but failing to load some plugins that I use on the home page.

If you can explain why this happens I would love to hear it. Failing that I would just like to strip off the query parameters entirely so that a request to:

http://mysite.com/?anything

would behave just like the request to:

http://mysite.com

Either through redirection or through simply removing the query string.

Here is my current .htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Handle redirection from https to http
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
# End https to http redirection

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^index.php$
RewriteRule . /index.php [L]

</IfModule>

# END WordPress
Chris S
  • 77,945
  • 11
  • 124
  • 216
Lothar_Grimpsenbacher
  • 1,677
  • 3
  • 19
  • 29
  • Before you "move on" would you please [mark an accepted answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)? – jscott Mar 23 '13 at 01:14
  • Well... it feels wrong to accept myself as the correct answer but since a mod edited my question to remove the answer I will add it below. – Lothar_Grimpsenbacher Mar 23 '13 at 03:59
  • It's "wrong" to leave a question "unaccepted" when you've got the correct answer. ;) Self-answer is encouraged, even rewarded, on SE sites. In fact, they build in [support for quicker handling](http://meta.stackexchange.com/questions/132886/what-is-this-answer-your-own-question-jazz) of such cases. – jscott Mar 23 '13 at 09:05

3 Answers3

1

Since I answered the question myself and then that was edited out of my question I will enter it as an "own answer".

Adding the below solved my issue:

RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule (.*) /$1? [R=301,L]
Lothar_Grimpsenbacher
  • 1,677
  • 3
  • 19
  • 29
1

Hey @Lothar_Grimpsenbacher,

So I think it might not be an .htaccess problem, might be a WordPress miss concept. I will try to explain briefly;

WordPress has some query_vars that comes from the variables, and if you have the "pretty" permalinks activated (looks like you do from the rules of your .htaccess), the problem would be that the ?anything is a GET parameter not expected, so WordPress might think it's a case for a 404 depending on what are the variables.

As far as I can tell I wouldn't mess with the .htaccess files just to solve this simple problem, might give you serious problems later on.

I think the solution for your problem lies around WordPress been able to see that the variable passed is valid, check the solution below:

function prefix_add_query_vars() {
    global $wp;
    $wp->add_query_var('anything');
}

add_filter('init', 'prefix_add_query_vars');

And within your template page you can check for the value like this:

if ( get_query_var('anything') ) {
    //do your stuff
}

PS: I'm not good with .htaccess file rules my self...

Webord
  • 161
  • 1
  • 6
0
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule (.*) /$1? [R=301,L]

(per the author)

Chris S
  • 77,945
  • 11
  • 124
  • 216