Apache non-www to www .htaccess redirect

We need to redirect all of our http://example.org URLs to http://www.example.org URLs instead. We're using Apache. How do we make this happen?

Well, .htaccess is your friend! Firstly, open up the public_html or document root of your website, either via FTP or if you've a control panel like cPanel or Plesk you can also use their inbuilt file managers. Once you're there, you'll almost certainly have an existing .htaccess file. If you do, simply open that file in your favourite text editor. Otherwise, please create a new .htaccess file.

The code you'll need to add is:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

One importance to remember is that .htaccess works in order of top to bottom. With this in mind, make sure you add the redirect code at the top of your file so as to ensure it is not overridden or conflicted by existing rules inside the .htaccess file.

  • 1 Users Found This Useful
Was this answer helpful?

Related Articles

Apache configuration for receiving reverse-proxied SSL traffic

Many customers use SSL-terminating reverse proxies and load balancers in front of their Apache...

Apache 2.4 .htpasswd protection and whitelisting multiple IP addresses

A customer asked us to secure their development environment using .htaccess and .htpassword...

Redirect http to https

Once you've got your SSL certificate installed, you'll want to switch your site traffic from http...

Optimise cPanel PHP-FPM performance

Activating a PHP-FPM pool for your cPanel domain will give a hefty performance boost over the...

Nginx-like "microcaching" using Apache mod_cache

Nginx is well known for its high performance 'microcache' feature, which is often used to make...