piyush23424 Posted April 19, 2010 Share Posted April 19, 2010 Hello i need a function that checks the url like www.sitename.com or sitename.com if it contains the "www." in the url then it should cut the "www." part from the url. Thanks Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted April 19, 2010 Share Posted April 19, 2010 $domain = $_SERVER['HTTP_HOST']; $path = $_SERVER['REQUEST_URI']; if($domain != 'site.com') { header("Location: http://site.com".$path.""); } Quote Link to comment Share on other sites More sharing options...
JustLikeIcarus Posted April 19, 2010 Share Posted April 19, 2010 Just create an .htaccess file in the web root. RewriteEngine On RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC] RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L] Quote Link to comment Share on other sites More sharing options...
piyush23424 Posted April 19, 2010 Author Share Posted April 19, 2010 $domain = $_SERVER['HTTP_HOST']; $path = $_SERVER['REQUEST_URI']; if($domain != 'site.com') { header("Location: http://site.com".$path.""); } I am not using this to redirect but there is a functionality in my site when i put www with domain name then it works and it doesn't work when i put the url like http://sitename.com Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 19, 2010 Share Posted April 19, 2010 If you are wanting to modify a URL that is not related to the URL the user is currently viewing (i.e. user entered value), then this will work: preg_replace("/www\./", '', $url); It has one flaw. If the url contains 'www.' anywhere in the url it will be removed. Ex: "subdomainwww.domain.com" would become "subdomaindomain.com". I did that so it would work for values such as "http://www.domain.com". But, I think the likelyhood of such an occurance would be very remote. However, if the values will always start from the subdomain (i.e. not 'http://') then this would be better: preg_replace("/^www\./", '', $url); Quote Link to comment Share on other sites More sharing options...
JustLikeIcarus Posted April 19, 2010 Share Posted April 19, 2010 Ok if you need the www. for the site to work then your apache rewrite rule would be this. RewriteEngine On RewriteCond %{HTTP_HOST} ^domain.com$ [NC] RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.