Apache non-www to www .htaccess redirect Print

  • 1

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.

Was this answer helpful?

« Back