5

This question is here so we can offer users who are looking for information on how to make one or more common or basic redirects in Apache using the htaccess file. All future questions pertaining to finding information that is that is covered by the question should be closed as a duplication of this question. As per this Meta question.

Whats the point in this question? The idea while not perfect is catch the most commonly asked questions regarding redirects using the htaccess on the Apache platform either on some type of lamp or a live server. The type of answers should be generally those that you could imagine are used by 100,000’s of sites world-wide and are constantly asked here at Pro Webmasters repeatedly over and over in various forms.

A few examples of the type of answers we are looking for:

  • How can I redirect non-www to www?
  • How can I redirect a sub domain to the main domain?
  • How can I redirect a sub folder from domain to a root or a subdomain?
  • How can I redirect an old URL to a new URL?

A few examples of the types of answers that we are not looking for:

  • Answers that do not involve a redirect.
  • Any answers relating to NGinx, IIS or any other non-Apache platform.
  • Answers that involve custom and complex string or query removals.

Resources for Advanced to Complex Mod_Rewrite Rules:

Please note: that this question is still in construction and may need some refining either by myself or a real moderator of Pro Webmasters, if you have any concerns or questions please use the meta page I made a few days back here.

1

1 Answer 1

8

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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.