wp plugin - remove www. from url

Date: 18th October 2007 at 6:11 pm | Filed under: development, plugins, wordpress | Author: Sam Burdge

A slight variation from my previous wordpress plugin 'Force www. in URL'.

This plugin removes the www. from the url regardless of what the user has typed or the link they have followed.This type of redirection is usualy done by altering the .htaccess file, which some Wordpress users may not have access to. Using a plugin to do this is an easy solution for WordPress users as the setup process is simple, and will be familiar to most of you.

Instructions:

  1. Download the plugin file here: Remove www. Plugin (rar) | Remove www. Plugin (zip)
  2. Upload the php file into your plugins folder (/wp-content/plugins/)
  3. Go to your plugins page in WordPress and activate the plugin

here's an example of the code:

/*Plugin Name: Remove www. from url
Plugin URI: http://www.samburdge.co.uk
Description: Removes www. from the url
Version: 1.0
Author: Sam Burdge
Author URI: http://www.samburdge.co.uk*/
function url_to_full (){
// read the host from the server environment
$host = $_SERVER["HTTP_HOST"];
$host = strtolower($host);
$host = trim($host);
$host = str_replace(':80', '', $host);
$host = trim($host);
$thesite = get_settings('siteurl');
$myarr = explode('://', $thesite);
$final = explode('/', $myarr[1]);
$no_dots= explode('.', $final[0]);
// if the host is not starting with www. redirect the
// user to the same URL but with www
if ($host == $final[0]){
// You an also change the "==" to "!=", if you want to force
// the user to use the domain name with the www.
// send status header, so that search engines or other services
// detect that this is a permanent redirect and not a temporary
header('HTTP/1.1 301 Moved Permanently');
// read the URL the user requested:
$url = isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : '';
// redirect the user to the new destination:
header('Location: http://'. $no_dots[1] . '.' . $no_dots[2] . '.' .
$no_dots[3] . '' . $url);
// stop the script execution here
exit;
}
}
add_action('init', 'url_to_full');

2 Responses to “wp plugin - remove www. from url”

  • Comment by Wouter
    Date: September 22nd, 2008 at 11:12 am

    Do you know how to undo this plugin? After activating this plugin I can’t login to my wp-admin anymore. I have Wordpress 2.6.2. When I try to go to wp-admin or wp-login.php it redirects me to http://nl../wp-admin instead of http://mydomain/wp-admin

  • Comment by Wouter
    Date: September 22nd, 2008 at 11:20 am

    By deleting the remove-www.php file my wp-admin works again so problem solved ;)

Leave a Comment