I would like to redirect my users from an old domain to a new one using htaccess but also add a variable in the address to indicate they are coming from the older domain,
here's an exampel
http://old.com/index.php?开发者_运维知识库var1=3 ==> http://new.com/index.php?var1=3&comingFromOld=1
http://old.com/index.php ==> http://new.com/index.php?comingFromOld=1
any help is greatly appreciated
Ok, so the normal basic redirect from old to new domains would be something like:
RewriteCond %{HTTP_HOST} ^old\.com$ [NC]
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L]
To add in this extra URL variables, there are two cases we want to consider. Firstly, some pages will already have URL variables, in which case we just want to append &comingFromOld=1. Secondly some pages won't have any URL variables, in which case we want to append with ?comingFromOld=1 instead.
You'd expect this would maybe require either two sets of rules, or one complicated regex to allow for both cases. Fortunately there's a nice flag we can use, QSA, that will append the original query string (if one exists). I think this should cover you.
RewriteCond %{HTTP_HOST} ^old\.com$ [NC]
RewriteRule ^(.*)$ http://new.com/$1?comingFromOld=1 [QSA,R=301,L]
So you'll either end up with URLs like http://new.com/index.php?comingFromOld=1
or http://new.com/index.php?comingFromOld=1&var1=3
精彩评论