Weblog Archive

Subdomain redirect

Tue, 8 Aug 2006 at 04:25 • Filed under Blog, Plugins, Server side

A couple of months ago, Matt released a simple plugin to redirect one’s site from the www. to no-www. I.e., If someone types http://www.ckunte.com in browser, they’d get redirected to http://ckunte.com. Matt’s code uses substr to strip the www. part of the URI (four characters, including the dot).

How does this interest me? Well, it’s nice to get an accurate report of stats rather than the same domain fragmented based on user’s query. In addition, I continue to get hits from my old address: http://chetan.ckunte.com. I wanted to change that and so, I did. Simple really.

While www. is 4 characters, chetan. is 7 characters. So, my updated plugin code based on Matt’s no-www looks like this:

  1. <?php
  2. /*
  3. Plugin Name: No Chetan
  4. Plugin URI: http://ckunte.com/
  5. Description: Chetan subdomain address is redirected to root.
  6. Author: Chetan Kunte based on Matt Mullenweg no-www
  7. Version: 1.0
  8. Author URI: http://ckunte.com/
  9. */
  10.  
  11. if ( !strstr( $_SERVER['HTTP_HOST'], 'chetan.' ) )
  12. return;
  13.  
  14. header('HTTP/1.1 301 Moved Permanently');
  15. header('Location: http://' . substr($_SERVER['HTTP_HOST'], 7) . $_SERVER['REQUEST_URI']);
  16. exit();
  17.  
  18. ?>
  19. Download this example: /inputfiles/subdomain-redirect.txt

(Seriously, no more posts after this for at least another 3-weeks, as I have been saying.)

Update: Did I mention that this technique also redirects all permalinks with old address to the new one?

[ Ads ]

Related posts

Following list is auto-generated, based on this post's context as possibly related. You may, however, occasionally find some in this list unrelated, but nevertheless, we sincerely hope that you'll enjoy them too.

Respond privately

Comments are closed, but you may respond privately to “Subdomain redirect.” (Your response will not be published.)