Give this a try, put this in your htaccess in the root of your site.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
replace example.com with your domain.
Question
I actually have PHP code for making 301 redirect on my site.Previously i done this by putting that PHP code on \public_html\site_name\index.php and that is not the same in www\site_name\index
(\public_html and \www are not same at that time).
But now these two folders(public_html and www) are showing the same content.Then now where i need to put the code?how to achieve 301 redirect using PHP when the *\public_html* and \www folders are in same.
La solution
Give this a try, put this in your htaccess in the root of your site.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
replace example.com with your domain.
Autres conseils
Create a .htaccess file in each of the directories with the folowing statement
Redirect 301 / http://anyothersite.com/