Query string breaking page

2019-08-07 05:41发布

A Google Analytics query string on an incoming link breaks the display of my site.

Does anyone know why the following would cause my site to fail to load its homepage properly (it appears to prevent the loading of some plugins that the page needs)

http://mysite.com/?utm_medium=email

While this does not:

http://mysite.com/#utm_medium=email

And if you know why, how do I fix this? I have incoming links that use the query string method and they are all resulting in the visitor seeing a broken homepage.

The site uses "pretty urls" so there are no query strings needed for the site routing. I tried simply removing the query string via apache rewrite (which is easy to do and works, I give an example below) but then I assume Google Analytics won't be able to track what it needs if I do this.

Here is my .htaccess in case that factors into it:

# 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]

# Example of what would strip query strings from url
#RewriteCond %{THE_REQUEST} \?[^\ ]+
#RewriteRule (.*) /$1? [R=301,L]

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

Extra

In addition to @faa's solution and incorporating @Eduardo's bit into this I also needed to exclude the admin directory from being affected. I did this by adding:

RewriteCond %{REQUEST_URI} !^\/*(wp-admin)

2条回答
甜甜的少女心
2楼-- · 2019-08-07 06:17

"While this does not: http://mysite.com/#utm_medium=email"

If using # works and is a valid option instead of the question mark in the queries, you may try something like this:

# Example of what would strip query strings from url
RewriteCond %{QUERY_STRING} .
RewriteRule ^(.*) /$1#%{QUERY_STRING}? [R=301,L,NE,NC]

Redirects

http://mysite.com/?query to

http://mysite.com/#query

查看更多
走好不送
3楼-- · 2019-08-07 06:37

Scripts should usually ignore query parameters that they are not expecting. This is a good rule of thumb, that one of your scripts is neglecting. You will have to track down which is the offending script and fix it yourself. Or provide more information here that would make it easier to troubleshoot. A url for a page where it can reproduced is a start, with a url we can at least isolate for a client or server side issue.

As a workaround you can use the Anchor instead:

http://mysite.com/#utm_medium=email

Just make sure that you configure Google Analytics to understand the parameters inside the anchor part of the url.

Just add the following before your _trackPageview call.

_gaq.push(['_setAllowAnchor', true]);

Reference: https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiCampaignTracking#_gat.GA_Tracker_._setAllowAnchor

查看更多
登录 后发表回答