Uniform website URLs using mod_rewrite

While some people might type in "www.deekayen.net", others, like myself, don't type "www." anymore. Besides, telling someone your website address, starting with "www." is clumsy. Some people even leave off a W to say "ww.example.com". Worse yet, I've seen people actually listen and only type two w's. I use an Apache mod_rewrite rule to send a permanent redirect to users who type the "www.".

RewriteCond %{HTTP_HOST} !^deekayen.net [NC]RewriteRule ^/?(.*) http://deekayen.net/$1 [R=permanent,L]

The first translates to "if the URL you are requesting doesn't start with deekayen.net," followed by the second line triggered by the first, "then you must load http://deekayen.net/ instead. $1 will include the filename you requested. This is a permanent thing, so get used to it, because otherwise I'll keep redirecting you."

This rule can also be handy if you have multiple domain aliases for the same website. For example, I don't tell anyone about deekayen.com, but www.deekayen.com and deekayen.com both redirect under this rule to deekayen.net.


Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <hr /> <a> <p> <em> <strong> <cite> <code> <blockquote> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
Your post will remain unpublished until David examines it himself to check for spam.