In Apache HTTP Last updated:
Share on:
Cloudways offers managed cloud hosting for any size business to host a website or complex web applications.

Redirecting requests from a non-preferred domain is important because search engines consider URLs with and without “www” as two different websites.

It creates a duplicate entry, which is not suitable for SEO.

I noticed this when I  analyzed my site with an SEO tool, thought to share it with you all.

There are many ways to redirect non-www to www or www to non-www. Let’s look at some of the easiest ways to get this done.

Apache HTTP

If you are using an Apache HTTP server then you may either achieve this redirection through .htaccess or by modifying httpd.conf file.

If you are on shared hosting then you may prefer doing .htaccess file but if you have full control on servers like VPS or Cloud then worth considering httpd.conf way. Whatever method you choose, take a backup of the file before modifying anything.

To redirect from www to non-www, you can add the following.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^ 
RewriteRule (.*)$1 [R=301,L]

And, to redirect from non-www to www, add the below.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^ 
RewriteRule (.*)$1 [R=301,L]

Save the file and restart Apache if used httpd.conf method.


To redirect from www in Nginx, you can add the following code in nginx.conf file under server block.

return 301 $scheme://$request_uri;

The following example is for redirecting from www to non-www.

server {
        return 301 $scheme://$request_uri;

And, below code is for redirecting from www to non-www.

server {
    return 301 $scheme://$request_uri;

Save the file and restart Nginx server.


Implementing redirection is very easy if you are using Cloudflare. You can take advantage of Page Rules to implement redirection.

  • Log in to Cloudflare and select the site where you want to put the redirection
  • Go to the Page Rules tab and create a new rule
  • Enter the URL and select setting as “Forwarding URL”
  • Select the status code (301 if you want it permanently)
  • Enter the target where you want to redirect
  • The following rule is to redirect everything from to

  • Once done, click Save and Deploy

And within a minute, your site redirection is live.


I hope the above helps you to put the redirection as you want. If you are using WordPress and looking to redirect more than www then check out this article to handle redirection in WP.

Share on:
  • Chandan Kumar
    Chandan Kumar is the founder of Geekflare. He’s helped millions to excel in the digital realm. Passionate about technology, He’s on a mission to explore the world and amplify growth for professionals and businesses.

Thanks to our Sponsors

More great readings on Apache HTTP

Power Your Business

Some of the tools and services to help your business grow.
  • The text-to-speech tool that uses AI to generate realistic human-like voices.

    Try Murf AI
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.

    Try Brightdata
  • is an all-in-one work OS to help you manage projects, tasks, work, sales, CRM, operations, workflows, and more.

    Try Monday
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.

    Try Intruder