Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
Share on:

Redirect non-www to www or www to non-www

apache www
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

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.

Thanks to our Sponsors
More great readings on Apache HTTP
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • Semrush is an all-in-one digital marketing solution with more than 50 tools in SEO, social media, and content marketing.
    Try Semrush
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder