Forum Moderators: phranque

Message Too Old, No Replies

301 Redirect For Query Strings

Trying to avoid duplicate content w/ (mod_rewrite)

         

DynamicNiches

9:52 pm on Mar 7, 2010 (gmt 0)

10+ Year Member



Hello,

To the best of my knowledge the following 301 redirect will work with only one search string that looks like: page.php?q=18

RewriteCond %{QUERY_STRING} ^q=18$
RewriteRule ^/page.php$ websitename/newname.htm? [L,R=301]


I am trying to redirect multiple search queries using one command. The website is already written from php to html using mod_rewrite but somehow the php version recently started to get crawled. They all have the same exact format. They look like:
page.php?q=word1-word2 (the html pages look like page.html?q=word1-word2)

How would I define one command in htaccess so I wouldn't have to manually list 1000's of redirects. Can I use a * to define this?

RewriteCond %{QUERY_STRING} ^q=word*-word*$
RewriteRule ^/page.php$ websitename/newname.htm? [L,R=301]

g1smd

9:57 pm on Mar 7, 2010 (gmt 0)

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



How are '18' and 'newname' related?

It is likely that Mod_Rewrite can have no idea how they are linked.

In that case you need a different solution. Rewrite (that's rewrite, not redirect) the requests to a special script that given 'number' looks up 'newname' in the database and then issues a PHP 'HEADER' command to redirect to the new URL.

DynamicNiches

10:44 pm on Mar 7, 2010 (gmt 0)

10+ Year Member



That was an example. '18' is the php version of the page and 'newname' is the same exact page but in html format.

A more accurate example for the website in question would be:

RewriteCond %{QUERY_STRING} ^q=green-widget$
RewriteRule ^/search.php$ websitename/search.html? [L,R=301]


I might be wrong (if I am, sorry for the stupidity) but wouldn't it redirect like this?:
/search.php?q=green-widget to--> /search.html?q=green-widget


Both /search.php?q=green-widget and /search.html?q=green-widget are already created and indexed. They are the same file but one is html and one is php.

I want first want to 301 redirect all php queries to their respective "duplicate" html queries. Meaning:
/search.php?q=red-widget to--> /search.html?q=red-widget
/search.php?q=purple-widget to--> /search.html?q=purple-widget
/search.php?q=blue-widget to--> /search.html?q=blue-widget

etc. and so on...

jdMorgan

12:54 am on Mar 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Redirect /search.php?q=<any-string> to /search.html?q=<any-string> in /.htaccess

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

If q= can be blank and should still be redirected, remove the "." from the end of the rewritecond pattern.

By default, the original query string is passed un-modified by RewriteRule.

Jim