Welcome to WebmasterWorld Guest from 54.145.173.36

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Facebook Query String.

301 duplicate homepage URL

   
11:49 am on Aug 4, 2010 (gmt 0)

10+ Year Member



We have the FB fan/like thinggy on our site and I noticed today that in WMT that a page with http://www.example.com/?_fb_q=1 is now a duplicate of our home page ("Google found the keyword widgets on these top pages:")

All I know is that this is something to do with Facebook and this URL is duplicating our home page. Gotta get its fixed.

I have tried the following 301 but I get 500 server error and would really appreciate any advice /syntax correction.

JDM, I found this on WebmasterWorld and tried to modify it ; ))

RewriteCond %{QUERY_STRING} ^q=.
RewriteRule ^search\.php$ http://www.example.com/search.html [R=301,L]

too:

RewriteCond %{QUERY_STRING} ^?_fb_q=.
RewriteRule ^?_fb_q=1$ http://www.example.com [R=301,L]

Thanks guys
1:36 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The pattern in the RewriteCond must match the query string only. The query string does not *start* with a question mark... That is, the question mark is not "included" in the query string, nor is it included in the URL-path -- It is a delimiter *between* these fields and part of neither. Compare my example pattern with yours...

The pattern in the RewriteRule must match the requested local URL-path only. It must not contain the protocol, hostname, or query string. If the requested URL-path is just "/", then the RewriteRule pattern should be "^$" (the leading slash is stripped in .htaccess).

Jim
1:57 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



Thanks Jim,

I have tried the following but as it does not redirect the URL from http://www.example.com/?_fb_q=1 to http://www.example.com/ I assume I have missunderstood your instructions.

This is what I have:

RewriteCond %{QUERY_STRING} ^q=.
RewriteRule ^$ http://www.example.com [R=301,L]

I dont want to redirect all query strings to the home page just ?_fb_q=1

Thanks again.
2:25 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Add a question mark to the end of the target URL to clear the appended query string.

Redirect for query string on both / and /index.php requests.

Redirect to root with trailing slash included.


RewriteCond %{QUERY_STRING} &?_fb_q=
RewriteRule ^(index\.php)?$ http://www.example.com/? [R=301,L]
2:51 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



I would never have gotten that but I think I now understand.

thanks Jim.
3:26 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If you only want to redirect queries starting with "_fb_q=" then use

RewriteCond %{QUERY_STRING} ^_fb_q=
RewriteRule ^(index\.php)?$ http://www.example.com/? [R=301,L]

If you want to redirect queries *containing* "_fb_q=" (possibly among other query name/value pairs), then use

RewriteCond %{QUERY_STRING} ^([^&]+&)*_fb_q=
RewriteRule ^(index\.php)?$ http://www.example.com/? [R=301,L]

This avoids a potential ambiguity in the use of a pattern such as "&?_fb_q=", which can match either
"<anything or nothing>_fb_q=" or "<anything or nothing>&_fb_q=".

Note that in all three of these patterns in this post, the "?" is not a literal. It is a regular-expressions quantifier meaning "Match zero or one of the preceding character." If you ever want to match a literal question mark character, then it must be escaped, as in "\?".

The question mark at the end of the RewriteRule substitution (which is not a regex pattern) is a different operator meaning "Replace the current query string with the query string that follows," which in this case, is blank.

Jim
4:07 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



I just realised there is a clash with the following line in the htaccess file

RewriteRule ^index\.(php|html|htm) http://www.example.com/ [R=301,L]

I guess its stuck in a loop. Can these two rules coexist?
8:10 pm on Aug 4, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



That newly-mentioned rule would be more robustly written as:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.(php|html?)\ HTTP/
RewriteRule ^index\.(php|html?)$ http://www.example.com/ [R=301,L]

However, there should be no clash, because neither of these rules redirects to "index.php"

Jim
8:27 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



Jim,

After a serious session of head scratching, trial and error your explanation has starting to sink in.

Based on your improved version I have managed to get these two rules to coexist with an additional RewriteCond and slight mod:

RewriteCond %{QUERY_STRING} ^([^&]+&)*_fb_q=
RewriteRule ^(index\.htm)?$ http://www.example.com/? [R=301,L]

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


Its basically a hack of a previous posts of yours but its actually works!

Note: I switched php to htm in first RewriteRule. (My server returns a 401 for /index.htm which is not ideal but better than a duplicate page).

Not very pretty I know but seems to do the job :)

you mentioned here: [webmasterworld.com...]

You do need a rewritecond to prevent an infinite loop if DirectoryIndex is set (as is customary) to include index.php.


Perhaps this would be a better solution but I could not figure out where to place the rewritecond.

As always, much appreciated.
8:34 pm on Aug 4, 2010 (gmt 0)

10+ Year Member



looks like we posted same time!

As expected your solution is nicer :)

RewriteCond %{QUERY_STRING} ^([^&]+&)*_fb_q=
RewriteRule ^(index\.php)?$ http://www.example.com/? [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.(php|html?)\ HTTP/
RewriteRule ^index\.(php|html?)$ http://www.example.com/ [R=301,L]


Works a charm. Your a star!

thanks Jim