How do I enable 301 redirecting using php & track the clicks on the link using google analytics? I figured out the code for redirecting should be <? header( "HTTP/1.1 301 Moved Permanently" ); header( "Status: 301 Moved Permanently" ); header( "Location: http://www.new-url.com/" ); exit(0); ?> PHP: But where do I put the google analytics tracking code?I need this for CB products & I don't want to use html or JS redirects as this may cause google penalty.
<?php echo <<<EOT /* Paste Google analytics code here */ EOT; header( "HTTP/1.1 301 Moved Permanently" ); header( "Status: 301 Moved Permanently" ); header( "Location: http://www.new-url.com/" ); exit(0); ?> PHP: The above code should work to keep track of stats.
no it doesn't. Once you put a 301 in the header the apache server should not render any other html for security reasons. Yes a 301 or 302 response can have html code in it acording to the RFC but in practice both apache and IIS send the headers only. Even if they did browsers don't display code in 301 redirects. They just follow them. And we all know javascript (like the one in google analytics code) is only executed when the page is displayed. Short version: you can't do that with server side redirects. Try meta or javascript.