How to redirect users to current group page after login

Hi there,

I wrote some code in my functions.php file that would allow users to log in from a buddypress group page, instead of seeing the "join group" button. When they log in, I would like them to be directed back to the same group page that they were on. Here is the code that I have written...right now, they get redirected to mysite.com/groups/ instead of mysite.com/groups/groupname. Thanks for any help!

echo '<div class="generic-button group-button public" id="groupbutton"><a href="'.site_url('/wp-login.php?action=login&redirect_to=' . get_permalink()).'" title="Sign in to join group" class="group-button join-group">Sign in to join group</a></div>';}