Page 1 of 1
redirect pages to www
Posted: Thu Nov 25, 2010 2:34 am
by shaam
Hi,
I want to redirect pages e,g '
http://mydomain.com/pagename.html' to '
http://www.mydomain.com/pagename.html",i.e if anyone access without www then it will be added automatically,server is php,how can i do this ?
plz help
Thanks
Re: redirect pages to www
Posted: Thu Nov 25, 2010 3:21 am
by Bind
Code: Select all
<?php
if(strpos($_SERVER['HTTP_HOST'],'www.')===false)
{
header ('HTTP/1.1 301 Moved Permanently'); # optional - to notify search engines and such
header ('Location: http://www.'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit;
}
?>
Re: redirect pages to www
Posted: Thu Nov 25, 2010 4:50 am
by shaam
Thanks for your reply,i have done this with different method using .htaccess,which is working fine,using following code.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$
http://www.mydomain.com/$1 [r=301,nc]
Re: redirect pages to www
Posted: Mon Nov 29, 2010 2:54 am
by shaam
How to do this with javascript instead of php ?
Re: redirect pages to www
Posted: Wed Dec 01, 2010 6:23 am
by Bind
shaam wrote:How to do this with javascript instead of php ?
Code: Select all
<script language="text/javascript">
var prot = location.protocol;
var host = location.host;
var path = location.pathname;
if(host.indexOf('www.') == -1)
{
location.replace(prot+'//www.'+host+path);
}
</script>