Forum Moderators: phranque

Message Too Old, No Replies

htaccess question mark removal & Google analytics?

how to remove question marks and keep the gclid?

         

wzshop

8:25 am on Jul 19, 2011 (gmt 0)

10+ Year Member



Hi all,
For SEO I use the following code in my htaccess, in order to remove all question marks and the parameters behind it.

#Redirect to remove query string from any directory-paths
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?#\ ]*)\?[^\ ]*\ HTTP/
RewriteCond $1 !^stats/
RewriteRule ^(.*)$ http://www.#*$!.com/$1? [R=301,L]


This makes all the question marks and its parameters to disappear. But this also removes the '?gclid' thing from the url (the extension that Adwords applies) to disappear. Therefore my paid clicks do not appear in Analytics. So is it possible to exclude this from removal?

Thanks a lot for yr help!

wzshop

7:48 pm on Aug 1, 2011 (gmt 0)

10+ Year Member



Ok, ill extensively come back on this tomorrow (i think we are in a different time zone;)).

Thanks again a lot! for your time.
Robbert

wzshop

8:51 am on Aug 2, 2011 (gmt 0)

10+ Year Member



Ok, the code that i have now is as follows:

RewriteEngine on


# www redirect
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

#remove phpsession
<IfModule mod_rewrite.c>

#remove PHPSESSID
RewriteCond %{QUERY_STRING} PHPSESSID=.*$
RewriteRule .* %{REQUEST_URI}? [R=301,L]
</IfModule>



# Capture glcid parameter and remove all preceding (and following)
# parameters when glcid is present in middle or at end of parameters.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/
RewriteCond $1 !^stats/
RewriteCond %{QUERY_STRING} &glcid=([^&]+)
RewriteRule (.*) http://www.example.com/$1?glcid=%1 [R=301,L]

# Capture glcid parameter and remove all following parameters
# when glcid is present as first parameter of multiple parameters.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/
RewriteCond $1 !^stats/
RewriteCond %{QUERY_STRING} ^glcid=([^&]+)&
RewriteRule (.*) http://www.example.com/$1?glcid=%1 [R=301,L]

# Remove all parameters except when glcid is present.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/
RewriteCond $1 !^stats/
RewriteCond %{QUERY_STRING} !glcid=
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

# Canonical redirect.
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1? [R=301,L]


RewriteRule ^computer-repair/(.+)\.html$ /region.php?regio=$1

ErrorDocument 404 /errors/404.php



This cause for the whole website to go down. Actually the error message i get in my browser is that there are too many redirects (310). I tried both of your solutions for the first 2 rules.

Hope you can help me further and i really appreciate it.
Robbert

g1smd

12:17 pm on Aug 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, the rules are in the wrong order. The non-www to www canonical redirect should always be the last redirect. Delete your www rule because there is already a rule in the right place to do that.

There's also one typo in
# Canonical redirect.
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$

There should be a
!
immediately before the
^
here.

You don't need a separate rule to remove session IDs. The other rules will remove them at the right time. Delete it.

Make sure that every rule has the
[L]
flag on the end. Your rewrite is missing it.

wzshop

2:58 pm on Aug 2, 2011 (gmt 0)

10+ Year Member



Okay thanks again.
Did all you said, another little typo was "glcid" instead of "gclid"(last one is correct).

Now the code i have is as follows

RewriteEngine on

# Capture gclid parameter and remove all preceding (and following)
# parameters when gclid is present in middle or at end of parameters.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/
RewriteCond $1 !^stats/
RewriteCond %{QUERY_STRING} &gclid=([^&]+)
RewriteRule (.*) http://www.example.com/$1?gclid=%1 [R=301,L]

# Capture gclid parameter and remove all following parameters
# when gclid is present as first parameter of multiple parameters.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/
RewriteCond $1 !^stats/
RewriteCond %{QUERY_STRING} ^gclid=([^&]+)&
RewriteRule (.*) http://www.example.com/$1?gclid=%1 [R=301,L]

# Remove all parameters except when gclid is present.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/
RewriteCond $1 !^stats/
RewriteCond %{QUERY_STRING} !gclid=
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

# Canonical redirect.
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1? [R=301,L]


RewriteRule ^computer-repair/(.+)\.html$ /region.php?regio=$1

ErrorDocument 404 /errors/404.php

# Index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]


Now no error message shows up anymore. Alle the redirects for excluding the question marks and stats folder etc. works also fine.

Now about the gclid thing:
-when i go to www.example.com?gclid=COb1wftMpogSWQJPQgodwXk53 it works! that means that the gclid thing stays in the url

-when i go to www.example.com/computer-repair/region1-region2-region3.html?gclid=COb1wftMpogSWQJPQgodwXk53 it redirects to http://www.example.nl/region.php

So there something goes wrong. Must have to do something with
RewriteRule ^computer-repair/(.+)\.html$ /region.php?regio=$1
right?

Hope you can find the error and help me out..
Thanks again! Robbert

g1smd

3:45 pm on Aug 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Apologies for the gclid/glcid typo. It's always a simple error that stops things working.

As stated above, you need the [L] flag on the end of every RewriteRule. It is missing from one.

The index redirect at the very end is in the wrong place. It is too late to redirect to a different URL after you already started to internally rewrite the request to a location on the server hard drive in the previous rule. What it is doing is exposing the rewritten path back out on to the web as a new URL.

The index redirect must be placed somewhere before the non-www/www redirect. However, depending where you put it you will introduce an unwanted double redirect, a redirection chain, for some requests.

The other rules could be amended or duplicated to remove the index.php part at the same time as removing the unwanted parameters, but you should not tackle any of that until the rest of the code is working 100%.

I don't see anything in that code that is capable of redirecting to "exactly"
http://www.example.com/region.php


There must be some other code in the sub-folder .htaccess file doing that.

[edited by: g1smd at 3:58 pm (utc) on Aug 2, 2011]

wzshop

3:58 pm on Aug 2, 2011 (gmt 0)

10+ Year Member



So it should be like this?


RewriteEngine on

# Capture gclid parameter and remove all preceding (and following)
# parameters when gclid is present in middle or at end of parameters.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/
RewriteCond $1 !^stats/
RewriteCond %{QUERY_STRING} &gclid=([^&]+)
RewriteRule (.*) http://www.example.com/$1?gclid=%1 [R=301,L]

# Capture gclid parameter and remove all following parameters
# when gclid is present as first parameter of multiple parameters.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/
RewriteCond $1 !^stats/
RewriteCond %{QUERY_STRING} ^gclid=([^&]+)&
RewriteRule (.*) http://www.example.com/$1?gclid=%1 [R=301,L]

# Remove all parameters except when gclid is present.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/
RewriteCond $1 !^stats/
RewriteCond %{QUERY_STRING} !gclid=
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

# Canonical redirect.
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1? [R=301,L]


RewriteRule ^computer-repair/(.+)\.html$ /region.php?regio=$1 [L]

ErrorDocument 404 /errors/404.php


? Still the same problem occurs. When i go to www.example.com/computer-repair/region1-region2-region3.html?gclid=COb1wftMpogSWQJPQgodwXk53 it redirects to http://www.example.nl/region.php

Furthermore, how/where can i cleanly do the index.php redirect?
Thanks so much! Robbert

g1smd

4:04 pm on Aug 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't see anything in that code that is capable of redirecting to "exactly"
http://www.example.com/region.php


There must be some other code in the sub-folder .htaccess file doing that.

lucy24

6:54 pm on Aug 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When i go to www.example.com/computer-repair/region1-region2-region3.html?gclid=COb1wftMpogSWQJPQgodwXk53 it redirects to http://www.example.nl/region.php

This is a little bit mysterious, since there is absolutely nothing in the quoted htaccess that would ever redirect to www.example.nl.

So either there is another part of the htaccess you're not quoting (it would say explicitly www.example.nl OR it would say www.example.$1 or www.example.%1 where the "nl" is being pulled from an earlier part of the rule) ... or there's additional business taking place in the php.

wzshop

9:13 am on Aug 3, 2011 (gmt 0)

10+ Year Member



Hello,

I do not have any other htaccess code, except some simple 301 redirects
Redirect 301 /old/old.html [examle.nl...]

The redirect to region.php only happens when i add the "gclid" thing.
It should have something to do with
RewriteRule ^computer-repair/(.+)\.html$ /region.php?regio=$1 [L]
right? Anyway, there is no other .htaccess in a subfolder. I also did not really find a php thing that is causing the page to go to /region.php...

Any ideas?

lucy24

6:31 pm on Aug 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ah ha.

g1 is now going to explain why it is A Very Bad Idea to have Rewrite and Redirect in the same .htaccess file.

g1smd

6:52 pm on Aug 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Take all the rules coded with Redirect and reformulate them as RewriteRule with the [R=301,L] flags. Ensure the redirect target includes the correct domain name. List those rules first.

Mixing Redirect and RewriteRule can result in rules being processed in the wrong order. Use RewriteRule for all of the rules.

wzshop

8:37 am on Aug 4, 2011 (gmt 0)

10+ Year Member



Ok, so the rewrite rule would look like this then?

RewriteRule /old/old.html http://www.example.com/new/new.html [R=301,L]
?

It will however result in the same problem, even when i delete all the other redirects out of my htaccess... urls like www.example.com/computer-repair/region1-region2-region3.html?gclid=213132132132

will all go to www.example.com/region.php

Urls like: www.example.com?gclid=123313232 do succeed...

g1smd

7:08 pm on Aug 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule cannot see the leading slash of URL request. Remove it and add the ^begins with token.

Escape all literal periods in RegEx patterns.

lucy24

7:13 pm on Aug 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The Redirects didn't have conditions, so you may now need to backtrack and make sure that each of the, er, rewritten redirects includes the part about gclid.
This 44 message thread spans 2 pages: 44