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
redirect pages to www
Moderator: General Moderators
Re: redirect pages to www
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
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]
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
Re: redirect pages to www
How to do this with javascript instead of php ?
Re: redirect pages to www
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>