Robo Robok Robo Robok - 1 year ago 148
HTTP Question

Is it common practice to redirect for skipping www?

I want to advertize my website as For me it looks nicer than Is it a common practice to trim this

part with redirection? Somebody would go to
and I would redirect him to
. Are there any potential traps behind this idea?

Another reason is making third party callback URLs simple. For now, I need to declare both
as my redirect URLs, because Google considers them different. It's not a huge deal, but something extra to take into account.

If it's safe to trim
, how can that be achieved? I'm using PHP on Apache. My current .htaccess looks like this:

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Answer Source

Should you use www?

You should normalize on either or, which you choose is mostly up to your preference. There are varying opinions on which is correct or which should be used. advocates that it is redundant to use the www subdomain as using the http protocol implies the web.

By default, all popular Web browsers assume the HTTP protocol. In doing so, the software prepends the 'http://' onto the requested URL and automatically connect to the HTTP server on port 80. Why then do many servers require their websites to communicate through the www subdomain? Mail servers do not require you to send emails to Likewise, web servers should allow access to their pages though the main domain unless a particular subdomain is required. on the other hand advocates using the www subdomain because it allows your website to be more flexiable. By using the www subdomain you can use things like CDN's / cloud hosting easier by using a CNAME in your dns records.

When using a provider such as Heroku or Akamai to host your web site, the provider wants to be able to update DNS records in case it needs to redirect traffic from a failing server to a healthy server. This is set up using DNS CNAME records, and the naked domain cannot have a CNAME record.

It can also help prevent unnecessary cookie traffic when you have static assets hosted on another domain.

One common web site optimization is to serve static content from a subdomain, such as If you are using www, then this is no problem; your site’s cookies won’t be sent to the static subdomain (unless you explicitly set them up to do so).

Once you've decided

You'll want to redirect all traffic to which ever version you end up using. With apache this can be done using mod_rewrite as shown in other answers:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

If you are able to edit the main apache configuration, another option is to setup two Virtual Hosts where one just redirects to the other. I do this for my site and it's worked well.

<VirtualHost *:80>

    Redirect permanent /
<VirtualHost *:80>
    DocumentRoot /var/www
    #The rest of your configuration