Support Online
Skip to main content

Redirecting from WWW to Non-WWW

In this guide, you will learn how to implement SEO-friendly 301 permanent redirect from www subdomain to main domain (non-www) on the Apache web server running on CentOS 7.

This redirect improves user experience and increases SEO performance by informing search engines that there is a single canonical address.

What Will You Learn in This Guide?

  • How to configure DNS records for www and non-www
  • Create a simple 301 redirect with Apache mod_alias
  • How to separate the VirtualHost structure correctly
  • Testing routing with curl
  • Why this method is correct in terms of SEO

1. Configuring DNS Records

First of all, both example.com and www.example.com addresses need to reach your Apache server.

✔ Required DNS A records

HostnameGenreTarget
@AServer IP address
wwwAServer IP address

Thanks to these two records, all requests will reach your Apache server.


2. Configuring 301 Redirect in Apache

We will do the redirection with the mod_alias module.
This method is simple, fast and SEO friendly.


Check that the 2.1 mod_alias module is active

httpd -M | grep alias_module

This command shows whether the module is active or not.

If the module is not visible, enable it:

echo "LoadModule alias_module modules/mod_alias.so" | sudo tee -a /etc/httpd/conf.modules.d/00-base.conf

2.2 Remove www alias from main VirtualHost

Open the main site configuration file:

sudo vi /etc/httpd/conf.d/ornek.com.conf

If the file contains the following line, remove it:

ServerAlias www.ornek.com

Because now www requests will be managed by a separate VirtualHost.

2.3 Creating 301 redirects with new VirtualHost

Create a new configuration file:

sudo vi /etc/httpd/conf.d/www.ornek.com.conf

Contents of the file:

<VirtualHost *:80>
ServerName www.ornek.com
Redirect permanent / http://ornek.com/
</VirtualHost>

This configuration directs all requests coming to the www domain to the main domain.

2.4 Check configuration and restart Apache:

sudo apachectl configtest

Expected output:

Syntax OK

Then restart Apache:

sudo systemctl restart httpd

3. Testing Routing:

curl -IL http://www.ornek.com

Meaning of parameters:

-I → only shows HTTP headers -L → follows redirects

Expected output:

HTTP/1.1 301 Moved Permanently
Location: http://ornek.com/

HTTP/1.1 200 OK

When you type www.ornek.com into the browser, it automatically appears in the address bar:

ornek.com

If you see it, the redirect is working successfully.

Frequently Asked Questions (FAQ)

Why are 301 redirects important?

It tells search engines that your site has a single canonical address. SEO authority is indivisible.

Should you use mod_alias> or >mod_rewrite>?

For simple redirects, mod_alias is faster and more readable. For complex redirects, mod_rewrite is preferred.

Is redirection still necessary if I use HTTPS?

Yes. Separate VirtualHost redirects must be made for HTTP and HTTPS.

What does Redirect permanent do?

It performs a permanent (301) redirect by preserving the path of the incoming request.

Example:

www.site.com/blog>

leads to:

site.com/blog>

Why do we use http:// in the redirect line?

Apache requests the full URL in the Redirect directive. If no protocol is specified, routing may not work properly.

Result

In this guide, you learned how to configure www → non-www 301 redirect with Apache on CentOS 7.

This setting:

Improves SEO performance Improves user experience Brings your site to a single canonical address

You can safely implement these operations in the GenixNode infrastructure and easily scale your projects.