Start a conversation

Basic .htaccess features: mod_rewrite redirection module

The mod_rewrite module is an indispensable mechanism for quickly changing URLs. The main advantage is the large number of redirection rules. The module is activated using the following directives:

RewriteEngine On 

Options FollowSymLinks

RewriteRule is one of the functional directives of this module. It sets the redirection rule that is executed if the specified conditions are met. Conditions are set using the RewriteCond directive. First, the conditions are set, only then the redirection rule is written.

Below are some examples of redirects using the mod_rewrite module.

1. Redirecting the page to a new domain (in this case, no conditions are required):

RewriteRule ^page1\.html$ [R=301]

2. Redirecting a site from a non-www domain to a www domain:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\..* [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]

3. Redirect via HTTPS protocol. These directives should be specified at the very beginning of the .htaccess file. Pre-installation of an SSL certificate is required.

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Choose files or drag and drop files
Was this article helpful?
  1. Alexey Momot

  2. Posted
  3. Updated