Forum Moderators: phranque

Message Too Old, No Replies

Rewrite url > Remove question mark and parameters

.htaccess

         

wzshop

1:33 pm on Jun 7, 2011 (gmt 0)

10+ Year Member



Hi everyone!
I have urls like: www.domain.com/computers/repair.html
Now urls like:
www.domain.com/computers/repair.html?page=computers&subpage=repair

are also accessible which affects my ranking in Google negatively.

So i would like to have a rewrite rule in my .htaccess file in order to rewrite/remove the question mark and everything behinds it.

Does anyone has a solution for this?
Thanks A LOT in advance.
Kind regards,
Robbert

g1smd

3:11 pm on Jun 7, 2011 (gmt 0)

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



Add a question mark to the RewriteRule target.

This clears the attached query string.

wzshop

4:55 pm on Jun 7, 2011 (gmt 0)

10+ Year Member



Hello.
First of all, thanks a lot for your comment.
I'm actually a total noop in ".htacces-things", so maybe you could give me an example of what it would look like?

Thanks you in advance!
Gr. Robbert

g1smd

5:16 pm on Jun 7, 2011 (gmt 0)

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



There's close on 5000 previous threads with examples of this code. :)

Let's see you best effort for discussion (as per the forum charter).

wzshop

7:37 pm on Jun 7, 2011 (gmt 0)

10+ Year Member



Thanks for your reply!

I have tried some, like this one:

# Remove question mark if blank query string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /[^?]*\?\ HTTP/
RewriteRule (.*) http://www.example.com/$1? [R=301,L]

The code above only removes the question mark and not the things behind it. The problem here is that i do not understand what i am doing at all. So i would really appreciate some assistance.

Thanks again.
Greets.

wzshop

2:23 pm on Jun 8, 2011 (gmt 0)

10+ Year Member



Ok well, i found this one. Can anyone tell me what this exactly does? because now my old mnm stats wont work anymore... Thanks.

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

g1smd

11:45 pm on Jun 8, 2011 (gmt 0)

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



You'll need a add an exception to NOT redirect stats URLs.

RewriteRule %{REQUEST_URI} !^/stats


or similar.

wzshop

5:52 am on Jun 9, 2011 (gmt 0)

10+ Year Member



thanks, that worked. Can you pls tell me what rewriteCond

%{THE_REQUEST} ^[A-Z]+\ /([^?#\ ]*)\?[^\ ]*\ HTTP/

does exactly?

wilderness

6:13 am on Jun 9, 2011 (gmt 0)

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



Begins with ^
any character a thru z; [A-Z]
+; mataches one or more of the characters in brackets (i. e., a-z)
\ ; escape followed by a blanks space
/; forward slash

([^?#\ ]; NOT-a-z followed a pound sign and a trailing blank space

You finish the remaining translation using the explanations provided in the regex example in the forum library [webmasterworld.com]

*)\?[^\ ]*\

wzshop

11:31 am on Jun 16, 2011 (gmt 0)

10+ Year Member



Thanks a lot all.
Now i have the following code:

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



This works fine to remove the questionmark thing, but now a new problem emerged: Adwords. Since Adwords adds '?gclid=' for tagging the adwords urls, the problem is now that my analytics wont recognize the adwords visitors anymore because of the filter.

Is there anyway to bypass this?
Thanks again..
Robbert

g1smd

6:37 pm on Jun 16, 2011 (gmt 0)

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



Just like you had an exclusion for when the path includes 'statsfolder', add an exclusion for when the query string includes 'gclid'.

wzshop

7:48 pm on Jun 16, 2011 (gmt 0)

10+ Year Member



thanks a lot for yr reaction. I tried:
#Redirect to remove query string from any directory-paths
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^?#\ ]*)\?[^\ ]*\ HTTP/
RewriteCond $1 !^statsfolder/
RewriteCond $1 !^gclid
RewriteRule ^(.*)$ http://www.domain.com/$1? [R=301,L]


but that is not working, since it still removes the '?gclid' thing from the url (the extension that Adwords applies) and if i add a question mark before the 'gclid' in the htaccess i get an Internal Server Error. So what should it look like?

Thanks again!

g1smd

8:06 pm on Jun 16, 2011 (gmt 0)

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



You code looks for gclid in the requested path.

It needs to look for gclid in the QUERY_STRING parameters.

wzshop

8:57 pm on Jun 16, 2011 (gmt 0)

10+ Year Member



Thanks again. Ive got this:


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


The ?gclid stays now in the url.. but all the question mark urls stay working (which should not be the case).. what is wrong?
thanks

wzshop

8:16 pm on Jun 20, 2011 (gmt 0)

10+ Year Member



please can someone help me out?
would really appreciate it!