Last active
August 29, 2015 14:13
-
-
Save briansteeleca/c378dccac81bde8418ac to your computer and use it in GitHub Desktop.
Handy .htaccess mod_rewrite rules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This tag ensures the rewrite module is loaded | |
<IfModule mod_rewrite.c> | |
# enable the rewrite engine | |
RewriteEngine On | |
# Set your root directory | |
RewriteBase / | |
# remove the .html extension | |
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP | |
RewriteRule (.*)\.html$ $1 [R=301] | |
# remove index and reference the directory | |
RewriteRule (.*)/index$ $1/ [R=301] | |
# remove trailing slash if not a directory | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteCond %{REQUEST_URI} /$ | |
RewriteRule (.*)/ $1 [R=301] | |
# forward request to html file, **but don't redirect (bot friendly)** | |
RewriteCond %{REQUEST_FILENAME}.html -f | |
RewriteCond %{REQUEST_URI} !/$ | |
RewriteRule (.*) $1\.html [L] | |
# redirect to preferred url - www.example.com | |
RewriteCond %{HTTP_HOST} ^example.com$ [NC] | |
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] | |
RewriteCond %{HTTP_HOST} ^example.ca$ [NC] | |
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] | |
RewriteCond %{HTTP_HOST} ^www.example.ca$ [NC] | |
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] | |
</IfModule> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment