Mod_alias and Mod_rewrite
There are 2 modules you can use within the htaccess for controlling redirects these are known as mod_alias and mod_rewrite. To help you establish the difference between the two below is some commands to familiarize when you're using mod_alias or mod_rewrite.
mod_alias commands
mod_rewrite commands
mod_alias is considered a simpler redirect method, while mod_rewrite is considered more advanced and allows more directive. It's worth noting that mod_rewrite commands are executed before mod_alias.
Redirect Codes
- 301 Permanent
- 302 Temporary
How to redirect a old URL to new URL?
There are many ways that you can redirect one page to another using the htaccess, 301 is generally the most important and used since it most often pages will need permanent redirects and not temporary. Permanent Redirects are great for attempting to keep rankings on one page to another.
301 Permanents
Redirect 301: Redirect 301 /old.html http://www.example.com/new-url/
RedirectPermanent: RedirectPerm /old.html http://www.example.com/new-url/
RedirectMatch: RedirectMatch 301 ^old.html$ http://www.example.com/new-url/
RewriteRule:
RewriteEngine On
RewriteBase /
RewriteRule ^old.html$ http://www.example.com/new-url/ [R=301,NC,L]
302 Temporary's
Redirect 302: Redirect 302 /old.html http://www.example.com/new-url/
RedirectTemp : RedirectTemp /old.html http://www.example.com/new-url/
RedirectMatch: RedirectMatch 302 ^old.html$ http://www.example.com/new-url/
RewriteRule:
RewriteEngine On
RewriteBase /
RewriteRule ^old.html$ http://www.example.com/new-url/ [R,NC,L]
Catch all and redirect non-www to www
You should opt to use mod_write for redirecting all requests for non-www versions of your site because the $1 variable will catch page names, so example.com/page1/
will automatically redirect to www.example.com/page1/
.
mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
mod_alias:
<If "%{HTTP_HOST} != 'www.example.com'">
Redirect permanent / http://www.example.com/
</If>
Catch all and redirect www to no-www
mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com$1 [R=301,L]
mod_alias:
<If "%{HTTP_HOST} != 'example.com'">
Redirect permanent / http://example.com/
</If>
Catch all and redirect www to no-www (HTTPS)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com$1 [R=301,L]
Enforce TLS on specific pages and disable on rest
This script will remove TLS on all other pages apart from the login page and register page, you can add more just use |
as the separator between file names.
mod_rewrite:
RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !\/(login|register)\.php [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
Enforce TLS on the entire site
If you want to enforce TLS on the complete site then you can use mod_rewrite to detect HTTPS off.
mod_rewrite:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Removing extensions from URL using mod_rewrite
Some web hosts may require Options -MultiViews
in your htaccess to remove these extensions, test it without and if it doesn't work simply add it.
Removing extension type PHP:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Removing extension type HTML:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Removing extension type HTM:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.htm [NC,L]
Not Working? Try this (change php to the file extension you want to remove):
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Removing and Enforce tailing slashes
Enforcing:
This will redirect all requests without a trailing /
to the URL with the slash on the end. (note within the 2nd part of the bracket is those file extensions to ignore... Since it wouldn't make sense to enforce /
on a picture URL.
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
Removing:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L
Convert backslashes into forward slashes
This will convert all backslashes found in a URL to forward slashes. Good for preventing duplicate content:
RewriteEngine On
RewriteCond %{REQUEST_URI} (.*)\\(.*)
RewriteRule .* %1/%2 [R=301]
Redirect Old Domain to New Domain
RewriteEngine On
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^sub-domain.example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [R=301,L]
Redirect to New Domain & Remove Sub Folder
RewriteEngine On
# Rename folder to match the sub folder you wish to remove
RewriteCond %{HTTP_HOST} ^(www\.)?example-old\.com$ [NC]
RewriteRule ^folder(/.*)?$ http://www.example-new.com$1 [L,R=301]