Removing WWW from URI (or domain) with PHP

Coding web page | Rolands Umbrovskis | April 20, 2010 4:40 pm 216 views | No Comments »

Few weeks ago I wrote how to remove WWW from domain with .htaccess. In some cases or hosting plans it’s not possible to view or use .htaccess file (e.g. some low cost or free hosting providers).

Solution is PHP code which redirects all www requests to non www page. Since You are (should) use some general configuration or header file in Your web application, you can include this PHP code (very top of php page):


<?php // Removing WWW from URI with PHP http://mediabox.lv/en/?p=136
if ('mediabox.lv' !== $_SERVER['HTTP_HOST']) {
header('Location: http://mediabox.lv' . $_SERVER['REQUEST_URI'], null, 301);
}
?>

It use build-in PHP header() function to redirect all requests to another page, in this case: non-www domain.

Simple!

Related Posts

Remove WWW from domain with .htaccess
Remove WWW from domain with .htaccess

Another good thing about removing double content is to remove or add www prefix to your domain. Remove WWW from domain [sourcecode...

Remove index.html and/or index.php from the URL
Remove index.html and/or index.php from the URL

Inspired by my discussion with Kaspars on his homepage konstruktors.com how to better remove index file. So I'll post both versions, because they...

Final cut for my first WordPress plugin
Final cut for my first WordPress plugin

More than one month ago I published my first WordPress plugin "Draugiem.lv/lapas fan page" for social network in Latvia draugiem.lv (FRYPE.COM)....

WordPress theme feature vs. WordPress plugin. What is best choice?
WordPress theme feature vs. WordPress plugin. What is best choice?

As WordPress developer at SimpleMediaCode.com I came to point where I have to decide: make some functions as WordPress theme features or make them...

Writing first public WordPress Plugin (widget)
Writing first public WordPress Plugin (widget)

Today was first release of  "Draugiem.lv/lapas Fan page" (for non-Latvian draugiem.lv best known as Frype.com) WordPress sidebar widget for FRYPE...

 
 
draugiem.lv/say SekoMan.lv twitter Digg this! Add to del.icio.us! Stumble this! Add to Techorati! Share on Facebook! Seed Newsvine! Reddit! Add to Yahoo!

Rolands Umbrovskis

Web developer at "Simple Media Code" / Mediabox.lv

Follow Rolands Umbrovskis on Twitter or Facebook

0 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Note: This post is over 4 months old.

 



bt bt bt bt
plugin by DynamicWP
#