Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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.

share|improve this question
    
Shouldn't this be a community wiki? –  w3d Apr 12 '13 at 22:12
    
yep, hopefully an mod can change it, don't think normal users can convert once posted. –  bybe Apr 12 '13 at 22:15
3  
    
That's awesome Michael, I'll tag that into the wiki. –  bybe Apr 16 '13 at 9:07

1 Answer 1

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 your 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 exercuted 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.domain.com/new-url/

RedirectPermanent: RedirectPerm /old.html http://www.domain.com/new-url/

RedirectMatch: RedirectMatch 301 ^old.html$ http://www.domain.com/new-url/

RewriteRule:

RewriteEngine On
RewriteBase /
RewriteRule ^old.html$ http://www.domain.com/new-url/ [R=301,NC,L]

302 Temporary's

Redirect 302: Redirect 302 /old.html http://www.domain.com/new-url/

RedirectTemp : RedirectTemp /old.html http://www.domain.com/new-url/

RedirectMatch: RedirectMatch 302 ^old.html$ http://www.domain.com/new-url/

RewriteRule:

RewriteEngine On
RewriteBase /
RewriteRule ^old.html$ http://www.askapache.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 varible will catch page names, so example.com/page1/ will automaticly 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 (very limited): Redirect 301 / http://www.domain.com/


Catch all and redirect www to no-www

mod_rewrite:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com$1 [R=301,L]

Enforce SSL on specific pages and disable on rest

This script will remove SSL on all other pages part 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]

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 tailing / 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.domain.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]

More to be added soon...

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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