Forum Moderators: phranque

Message Too Old, No Replies

Redirect according to specific query string parameter

redirects needed after sitemap correction

         

jshare

5:11 pm on Mar 13, 2007 (gmt 0)

10+ Year Member



My blog's sitemap was incorrectly showing tag urls of this format:
http://example.com/blog/index.php?tag=example

In reality, the tag urls are of this format:
http://example.com/index.php?tag=example

I've since corrected the sitemap generator so that its tag urls are of the correct format, but Google has already indexed some of the bad urls. I'd like to redirect bad url to good url while preserving the query string.

I tried a number of things and this is what makes the most sense to me, but it doesn't work:
RewriteRule ^blog/index\.php\?$ index.php? [R=301,QSA,L]

Any suggestions? Thanks in advance!

Jacob

[edited by: jdMorgan at 5:38 pm (utc) on Mar. 13, 2007]
[edit reason] No sigs or URLs, please. See TOS. [/edit]

jdMorgan

5:50 pm on Mar 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule does not 'see' the query string attached to the URL-path. You must use a RewriteCond to check/test the query string:

RewriteCond %{QUERY_STRING} ^tag=example$
RewriteRule ^blog/index\.php$ http://www.example.com/index.php?different_name=value [R=301,L]

If you don't need to change the query data, then this will suffice, leaving the query data unchanged:

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

And finally, for the case where you don't care whether any query data is present, you can just use, which would again leave the query data unchanged if it was present:

RewriteRule ^blog/index\.php$ http://www.example.com/index.php [R=301,L]

Note the proper form for a redirect URL, and the elimination of the unnecessary [QSA] flag on the rules.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

jshare

9:49 pm on Mar 13, 2007 (gmt 0)

10+ Year Member



Thanks jd, this is what worked:
RewriteCond %{QUERY_STRING} ^.*tag.*$
RewriteRule ^blog/index\.php(.*) /index.php [R=301,NC,QSA,L]

I appreciate it

Jacob

jdMorgan

10:02 pm on Mar 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If "tag" can appear anywhere in the query string, then don't anchor it. Just use:

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

The advantage is that in any character precedes "tag" it must be an ampersand, and if any characters follow, they must be in the form "=<value>", where <value> is any number of characters that are not an ampersand. This prevents failures of your rule for queries like "newtag=blue" or "item=stag-horn-pipe".

Again, use a full URL for the substitution path, and there is no need to include the [QSA] flag -- It is completely redundant in this case.

Jim