Merging www. and non-www. into one domain - 301 redirect via mod_rewrite

Practice



So, in Apache we have a very handy tool called mod_rewrite. And there's also a desire to make your domains www.domain.bla and domain.bla be the same domain as far as search engine crawlers are concerned.

The thing is that the addresses:
www.mydomain.ua
mydomain.ua
despite being hosted on the same server, having the same content, and generally being served from the same folder — are actually different domains. And search engines, accordingly, index them as different sites, which hurts your ranking.

So let's use this wonderful tool, mod_rewrite, and make our www and non-www domains the same site as far as search engines are concerned.

To do this, we'll send the crawler a message (301 Moved Permanently) for one of the variants, pointing to the address of the second variant. This way, the search crawler will remember that www.domain.bla has moved, say, to domain.bla, and will consider it one site.

So, to carry out this procedure, we need to create (or edit, if already created) a file called ".htaccess" (without the quotes, of course) in the root of our site.


Making www.domain.x the primary one

Add these lines to the .htaccess file so that visitors going to the address:
mydomain.ua
are always redirected to the address
www.mydomain.ua

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^mydomain.ua [nc]
RewriteRule ^(.*)$ http://www.mydomain.ua/$1 [r=301,nc]

This way, the primary domain will be www.mydomain.ru


Making the non-www domain the primary one

Add these lines to the .htaccess file so that visitors going to the address:
www.mydomain.ua
are always redirected to the address
mydomain.ua

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{http_host} ^www.mydomain.ua$
RewriteRule ^(.*)$ http://mydomain.ua/$1 [R=301,L]

In this variant the primary address will be mydomain.ru, and everyone who goes to www.mydomain.ru will end up at the address mydomain.ru


New domain name

Sometimes a site's domain is planned to be changed. Suppose you know this in advance and want to move the domain ahead of time. This is good in the sense that the search crawlers will have time to work, scan your site, and find out that it has moved.

In this case, add the following entries to the .htaccess file

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://otherdomain.ua/$1 [R=301,L]


Now everyone who visits your site at the old address will be unconditionally redirected to the new address otherdomain.ua

Applies to: Apache 2.x

created: 2017-05-09
updated: 2026-03-10
396



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Running server side scripts using PHP as an example (LAMP)"

Terms: Running server side scripts using PHP as an example (LAMP)