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 Link to comment https://forums.phpfreaks.com/topic/199016-need-to-trim-the-string/ 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.""); } Link to comment https://forums.phpfreaks.com/topic/199016-need-to-trim-the-string/#findComment-1044612 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] Link to comment https://forums.phpfreaks.com/topic/199016-need-to-trim-the-string/#findComment-1044613 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 Link to comment https://forums.phpfreaks.com/topic/199016-need-to-trim-the-string/#findComment-1044614 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); Link to comment https://forums.phpfreaks.com/topic/199016-need-to-trim-the-string/#findComment-1044615 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] Link to comment https://forums.phpfreaks.com/topic/199016-need-to-trim-the-string/#findComment-1044616 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.