Redirect domain without WWW with htaccess

There is a common problem that arises when we acquire our domains: duplicate content is easy to appear and it is a problem if we want to position ourselves properly in Google and CIA.

This problem is because when we assign the domain to our servers and folders with their proper DNS we get two links for the same content: one link with www and one without www.


  • What is duplicate content?
    • But I don’t copy content. How can it affect me?
  • How do I delete the domain without www?
  • Redirection with .htaccess file
  • Extra: Check mod_rewrite module

What is duplicate content?

Duplicate content is the problem that occurs when two web pages, or rather, two links, have the same content or at least share a great similarity (this resemblance is decided by search engines).

It is a serious problem that search engines try to solve by invalidating content that is marked as duplicate, which means that page will never rank.

But I don’t copy content. How can it affect me?

Although a priori any honest owner of a website may believe that it is impossible for duplicate content to affect him, there is a very common case that is always true: your domain with www. and the one who doesn’t have it.

If when you purchased and configured your domain nor you deactivated one of the two possible domainsyou should know that you most likely have a duplicate content problem that must be harming the positioning of your website.

How do I delete the domain without www?

To eliminate one of the domains that we have discussed, we must use the concept of redirection. In the case of this tutorial I will redirect domain without www to the one who does have it.

Redirecting or redirecting a link to another is the action of taking visits to a page of our website (or the entire domain) to another page or link.

To redirect these visits and avoid duplicate content, we are going to use the .htaccess configuration file, a file that allows us to apply programmed rules that will be executed before any other script on our websites.

He .htaccess file (hypertext access) is an Apache server configuration file that allows webmasters to apply access restrictions to directories or files, page redirection, access denial by IP, etc. All this with the idea of ​​improving the security of the web or improving the distribution of its content.

We will program in this .htaccess file with its own language, helping us with regular expressions to achieve a 301 redirect.

Redirection with .htaccess file

The code that will deal with the problem and launch the redirect or url rewrite is simple and consists of the following five lines:

#ifModule mod_rewrite.c
RewriteEngine On
#para redirigir a www
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301,NC]

You do not understand anything? [email protected]we are going to see line by line the written code.

  1. The first line is a conditional, yes, like a if of the .htaccessthis if is conditioning the execution of the entire redirection, which will only be checked if the module mod_rewrite is enabled on the Apache server.
  2. In case the module mod_rewrite of Apache is enabled, the following line will be read: Rewrite Engine On. This command activates the rewriting of the visited web address in the current context. This means that it will allow our next redirect code to be launched.
  3. RewriteCond is a module-specific conditional mod_rewrite. This conditional allows us to indicate a condition using regular expressions (RegExp) that will establish under what circumstances the next RewriteRule will be fired. The regular expression %{HTTP_HOST} indicated in the condition may sound familiar to you, it is normal, since this special variable of the file .htaccess refers to the same as in php the variable $_SERVER[‘HTTP_HOST’] to the domain of the url in question. The code: %{HTTP_HOST} ^ exactly what it says is that if the variable HTTP_HOST starts (^) with the condition will be correct and will evaluate to true. Finally, at the end of the line you will see [NC]this is a flagspecifically the flag NOCASE, a special variable that indicates that we don’t care if they are uppercase or lowercase. It does not matter if they write SRcodigoFuente.ES, or srcodigofuente. It is, the two possibilities will be evaluated as
  4. RewriteRule is the rewrite rule that will be executed if the previous RewriteCond evaluates to true. Once again use regular expressions to indicate the redirection to the url$1. Where $1 is any information that could appear in the url of the web after the domain ^(.*)$. An example of $1 would be the slug tutorials at ( Below, once again, I have indicated three configuration flags:
    1. L, which indicates that this rule will be the last to be executed, that is, any other rule that you include after this line will no longer be checked.
    2. R=301, type 301 redirection, indicating the type of rewrite the module has to perform mod_rewrite. There are other types, and rewriting does not entail redirection, which is forwarding the client’s request to another url.
    3. finally one more time NC to ignore possible case in the url.

All this code must be included in a file named .htaccess and include it in the root folder of your project.

Extra: Check mod_rewrite module

Although in practically all the hosting services that we contract, the mod_rewrite module It will come activated, this is not usually the case in local installations. To ensure that the previous redirect code works and does not leave our website blocked and with our ass in the air, we are going to check if we have the mod_rewrite module activated, and if not, activate it. This Apache configuration will be applicable to both installations of XAMPP like WAMPP.

We must go to our Apache installation directory: in XAMPP c:/xampp/apache/conf/ and open with your preferred text editor (I recommend notepad++) the httpd.conf file.

In Wamp we will find the httpd.conf file in: c:/wamp/bin/apache/apacheX.XX/conf.

We look for the line where it indicates mod_rewrite (in notepad++ with ctrl+f or cmd+f on Mac) and if we find the line with a hash in the first character it is disabled, we remove the # to uncomment the line, save and restart the server.

The specific line will look like this: LoadModule rewrite_module modules/

Finally I leave you a video so you can see graphically how to activate it!

!! Congratulations!! Everything should go smoothly, you can now try a in your url and test the operation of the .htaccess 301 redirect. You will see how it redirects you to the correct url.

Leave a Reply