View Full Version : Code for redirecting your domain using htaccess & getting rid of www

16th October 2006, 02:41 AM
Hey guys,

This is the code to get your domain to automatically forward from www.x something.com
to just

x - - Something.com

Visually I don't like seeing www in front of my IDNs & I'm sure there is going to be someone out there who feels the same.

Use notepad on Windows or I suggest BBedit or Dreamweaver for a Mac.

Name a file htaccess.txt
Put it in your site root

This is the Code

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www.xn--yourdomain.com [nc]
rewriterule ^(.*)$ http://xn--yourdomain.com/$1 [r=301,nc]

After you upload it to your server change the file on the server's name to .htaccess

& after that your domains will just be
http:// YOURIDN.com
instead of
http:// www,YourIDN.com

& don't thank me be nice to the little woman tonight instead.

16th October 2006, 04:01 AM
Or if you don't want to edit a crap load of htaccess files, you can stick it in your httpd.com file, outside of the virtualhost sections so that it's the default for all domains...

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} /^www.(xn--.+)$/ [nc]
rewriterule ^(.*)$ http://%1/$1 [r=301,nc]

(I just threw that together without checking the apache manual, so test it first I may have not quite got it right)

16th October 2006, 04:29 AM
Here is what I use on a per domain basis. The nice thing is that it sends the requesting host a 404 code, which means page not found, which prevents search engines from indexing the 'www.' site and removes it from their index if it exist. At the same time, the 404 error page is defined as being the same as the top page, so the user still gets to the main page, despite that fact that a 404 error was sent. Plus you can track who is visiting the 'www.' addess easily by checking the logs, which will also show a referrer being sent from the nonexistent 'redirect' directory that caused the 404 error.

This is for your httpd.conf file. (With hundreds of domains being served, it is easier to edit in all commands into that one file, and turn off individual .htaccess files entirely, speeds up the server also.)

<VirtualHost *:80>
DocumentRoot "/home/domain/html/redirect/"
#since the directory 'redirect' doesn't really exist it goes to the 404
ServerName www.domain.com
CustomLog logs/domain.txt combined

<VirtualHost *:80>
DocumentRoot "/home/domain/html/"
ServerName domain.com
CustomLog logs/domain.txt combined

<Directory /home/domain/html>
Options Indexes FollowSymLinks ExecCGI Includes
RewriteEngine on
ErrorDocument 404 http://domain.com/