Rewrite Rules

All websites use either www.website.com or website.com, for example. But still, Google penalises this for duplicated content reasons and hence the owner of a website is supposed to restrict access to only one. Using the RewriteEngine module, you can easily achieve this.

Create a 301 redirect which will force all HTTP request to use either www.website.com or website.com through rewrite rules.

Example

Redirect website.com to www.website.com

The first line tells apache to start the rewrite module.

RewriteEngine On

The next line specifies that the next rule only fires when the HTTP host does not include “!” the www.website.com.

 RewriteCond %{HTTP_HOST} !^www.website.com$ [NC]

The $ means that the host ends with www.website.com; the result is that all pages from www.website.com will trigger the following rewrite rule. To enforce the domain redirect to www.example.com, add the inversive “!”.

The [NC] specifies that the HTTP host is case insensitive.

The final line describes the action that will take place:

RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

The ^(.*)$ is important to note, can be any character (but only one). So (.*) means that you can have a lot of characters, not only one. These symbols are what we need because ^(.*)$ contains the requested URL, without the domain.

The next section HTTP://www.example.com/$1 describes the target of the rewrite rules. This target is the final used domain name, where $1 contains the content of the (.*).

The next part is crucial because it does the 301 redirects automatically: [L,R=301]. L means this is the last rule in this run. After this rewrite, the webserver will return a result. The R=301 implies that the response returns a 301 moved permanently to the requesting browser or search engine.

Using Rewrite Rules to send user requests to a new site

To redirect users to your intended website from the one they have visited, you should apply a 301 moved permanently redirect from the former domain to the new one. The advantages are:

  • Users will redirect automatically
  • Search engines will redirect to the new domain, and all relevant information transfers to the new domain 
  • Google information such as PageRank transfers to the intended domain name.

Adding a trailing slash

Some such engines, e.g. Yahoo remove the trailing slash from URLs that look like directories, but this could result in duplication of content if the same page is accessible from different URLs. 

Example

Assume website.com/google/ is indexed in Yahoo as website.com/google. The ‘/’ results in 2 URLs with the same content. The solution here is to create/make .htaccess rewrite rules that add trailing slashes to these URLs, and this thereby redirects the URLs that do not have the trailing slash to URLs that have one.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !website.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://website.com/$1/ [L,R=301]
Bash Scripting
Shell, Scripting & Bash Meaning Shell Meaning Shell is a macro processor […]
Red Hat Ansible Automation
If you need to manage your firewalls using ansible, below, you will […]
Rectenna - create power through wifi
“Rectenna” now that is a name to remember. Scientists are raising the […]