Update: .htaccess Code Generator
I have created a little program that will create .htaccess redirects for you. It is specifically designed to generate SEO-friendly redirects. Try it out.
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
- Wrong:
/file.html/
- Right:
/directory/
- Wrong:
/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.
You can also learn more about redirects in this chapter Introduction to .htaccess redirects.
.htaccess Code Samples
The following are all the pre-built configurations to use in your .htaccess file.
Variations
- https, www, no trailing slash on files
- https, non-www, no trailing slash on files
- http, www, no trailing slash on files
- http, non-www, no trailing slash on files
1. https, www, no 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
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://www.example.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://www.example.com/$1/ [R=301,L]
# Force HTTPS and WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
2. https, non-www, no 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
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://example.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://example.com/$1/ [R=301,L]
# Force HTTPS and remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
3.http, www, no 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
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ http://www.example.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ http://www.example.com/$1/ [R=301,L]
# Force HTTPS and WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
4. http, non-www, no 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
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ http://example.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ http://example.com/$1/ [R=301,L]
# Force HTTPS and remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]