Sample Code

Redirect Sample Code

There is no one .htaccess file rule that will work for every website. I understand that you may not want to prepend www. to your domain. To save you the time I have created a different variation for each possible configuration.

Each of these configuration samples will create a single redirect to the appropriate URL.

Note:

It is strongly recommended that you include a trailing slash on the end of directory URLs and not on the end of file URLs. This the right way to do it.

  • Right: /file.html
  • Right: /file.html/
  • Right: /directory/
  • Right: /directory

All the provided .htaccess samples correctly apply the trailing slash. For the time being this is the only method provided.

For more information about how this solution works please read the htaccess For SEO Guide section on How to force https, www and a trailing slash with one redirect.

.htaccess Code Samples

The following are all the pre-built configurations to use in your .htaccess file.

Variations

  1. https, www, no trailing slash on files
  2. https, non-www, no trailing slash on files
  3. http, www, no trailing slash on files
  4. http, non-www, no trailing slash on files

1. https, www, trailing slash on files

This set of .htaccess rewrite rules will force the site to use https as well as the www. subdomain. It will also ensure that there is a trailing slash only on directory URLs. This will all be done with only one redirect.

#### Force HTTPS
#### Force WWW
#### Remove trailing / from file

## Turn on rewrite engine
RewriteEngine on

## Check if not directory and ends in /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
## If not a directory skip next RewriteRule
RewriteRule ^ - [S=2]

## Check if HTTPS and WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off

## This RewriteRule skipped if URI was a directory
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

## This RewriteRule used if URI was a directory
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [S=1]
RewriteRule ^(.*)/$ https://www.example.com/$1 [R=301,L]

2. https, non-www, trailing slash on files

This set of .htaccess rewrite rules will force the site to use https as remove the www. subdomain. Finally, it will ensure that there is a trailing slash only on directory URLs. This will be accomplished with a single redirect.

#### Force HTTPS
#### Remove WWW
#### Remove trailing / from file

## Turn on rewrite engine
RewriteEngine on

## Check if not directory and ends in /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
## If not a directory skip next RewriteRule
RewriteRule ^ - [S=2]

## Check if HTTPS and WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off

## This RewriteRule skipped if URI was a directory
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

## This RewriteRule used if URI was a directory
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [S=1]
RewriteRule ^(.*)/$ https://example.com/$1 [R=301,L]

3.http, www, and trailing slash on files

This set of .htaccess rewrite rules will force the site to use the insecure http protocol. It will also force the www. subdomain to be used. Lastly it forces a trailing slash only on directory URLs. This is done with a single 301 redirect.

#### Force HTTP
#### Force WWW
#### Remove trailing / from file

## Turn on rewrite engine
RewriteEngine on

## Check if not directory and ends in /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
## If not a directory skip next RewriteRule
RewriteRule ^ - [S=2]

## Check if HTTPS and WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} on

## This RewriteRule skipped if URI was a directory
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

## This RewriteRule used if URI was a directory
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [S=1]
RewriteRule ^(.*)/$ http://www.example.com/$1 [R=301,L]

4. http, non-www, trailing slash on files

This set of .htaccess rewrite rules will force the site to be without https or www.. It will be served over the insecure http protocol. It will also ensure that there is a trailing slash only on directory URLs. These rules will cause only one redirect.

#### Force HTTP
#### Remove WWW
#### Remove trailing / from file

## Turn on rewrite engine
RewriteEngine on

## Check if not directory and ends in /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
## If not a directory skip next RewriteRule
RewriteRule ^ - [S=2]

## Check if HTTPS and WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} on

## This RewriteRule skipped if URI was a directory
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

## This RewriteRule used if URI was a directory
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [S=1]
RewriteRule ^(.*)/$ http://example.com/$1 [R=301,L]

Daniel Morell

I am a web developer and SEO with a focus on creative design, a passion for perfection, and an organic marketing green thumb.

© 2018 Daniel Morell.
+ Daniel + = this website.