Home  »  Htaccess  »  301 Redirect Cheatsheet

by 16 comments

301-redirectsI got tired of always having to search google to remember the syntax for issuing seo friendly (300, 301, 302, 303, 305, 306, 307) Redirects using multiple programming languages and methods... so I made my own list!

Redirection Methods

Am I missing something? Most definately! Please use the comment form below to suggest new code.

Redirect with meta http-equiv tags

These go in the section of your html. Usually when you use this meta redirect method you should also use the javascript method, just to be safe.

Refresh meta http-equiv tag

Redirects to after 0 seconds.

Redirect with javascript

These go in the section of your html.

Basic javascript Redirect method

Will redirect user to immediately

Redirect after specific time period

This will redirect to after 2 seconds

Redirect with PHP

Using "Location" header

Using "Refresh" header

Ultimate PHP method

It redirects to a page specified by "$url". $mode can be:

  • LOCATION: Redirect via Header "Location"
  • REFRESH: Redirect via Header "Refresh"
  • META: Redirect via HTML META tag
  • JS: Redirect via JavaScript command

Redirect with Perl

Simple 301 Redirect with perl

#!/usr/bin/env perl
my $URL = "";
print "Status: 301 Moved\r\nLocation: $URL\r\n";

Simpler 301 Redirect with perl

#!/usr/bin/env perl
print "Status: 301 Moved\r\nLocation:\r\n";

Redirect with ColdFusion (CFM)

Using cheader for 301 "permanent" redirect

Using clflocation for 302 "temporary" redirect

Redirect with ASP (VB Script)

301 Redirect

<%@ Language=VBScript %>
response.status="301 moved permanently"
Response.AddHeader "Location", ""

Redirect with shell script

301Redirect with sh shell script

#!/bin/sh -p
echo -e "Status: 301 Moved Permanently\r\nLocation:\r\n"
echo -e "Content-type: text/html\r\n"
exit 0;

Redirect with mod_rewrite in apache htaccess

See also Ultimate Apache htaccess article and URL Redirection

301 Redirect

Redirects permanently when a request is made to domain.tld/301-redirect.html to

RewriteEngine On
RewriteBase /
RewriteRule ^301-redirect\.html$ [R=301,NC,L]

302 Redirect

Redirects temporarily when a request is made to domain.tld/301-redirect.html to

RewriteEngine On
RewriteBase /
RewriteRule ^301-redirect\.html$ [R,NC,L]

Redirect with mod_alias in apache htaccess

301 Redirect

Redirect 301 /301-redirect.html

or with RedirectPerm

RedirectPerm /301-redirect.html

or with RedirectMatch

RedirectMatch 301 ^301-redirect\.html$

302 Redirect

Redirect 302 /301-redirect.html

or with RedirectTemp

RedirectTemp /301-redirect.html

or with RedirectMatch

RedirectMatch 302 ^301-redirect\.html$

Redirect with ErrorDocument in apache htaccess

302 Redirect

Issues a 302 Redirect to when a file is not found. See also Force Apache to output any HTTP Status Code with ErrorDocument

ErrorDocument 404

Redirect with Python / Django

Thanks to thebjorn for contributing this first one, which issues a 302 Redirect.

from django import http
def view(request):
return http.HttpResponseRedirect('/')

This example was contributed by John and shows how to issue a proper 301 Redirect, additional info on this below the example.

from django import http
def view(request):
return http.HttpResponsePermanentRedirect ('/')

Django includes a number of HttpResponse subclasses that handle different types of HTTP responses. Like HttpResponse, these subclasses live in django.http.

The constructor takes a single argument -- the path to redirect to. This can be a fully qualified URL (e.g. '/htaccess/') or an absolute URL with no domain (e.g. '/htaccess/'). Note that this returns an HTTP status code 302.
Like HttpResponseRedirect, but it returns a permanent redirect (HTTP status code 301) instead of a "found" redirect (status code 302).
The constructor doesn't take any arguments. Use this to designate that a page hasn't been modified since the user's last request (status code 304).

More Redirect Info


November 29th, 2007

Comments Welcome

  • Ash Nallawalla

    Thanks for this fantastic checklist! I will send my students to this page.

  • AskApache

    @ Ash

    Thanks! I will have to go through and clean up the code, especially adding code to do 307 Temporary Redirects, which should almost always be used instead of a 302 Redirect.

  • thebjorn

    Thanks for the list! saved me a bunch of time. If you'd like to add the code for Django/Python it is:

    from django import http
    def view(request):
    return http.HttpResponseRedirect('')

  • nBridges Media

    Thanks for such nice list

  • David Simmons

    Excellent collection! Thx

  • Natasha

    This is great information. Thanks a bunch

  • Luis

    Which would be the preferred redirect method: PHP or Mod Rewrite ? Why?
    I'm just curious abut it.

  • Mara Alexander

    The links under the "Redirect Method" section appear to have been meant to be anchor links, yet they all redirect to your main page ( I'm thinking the anchor tags on the page weren't done correctly.

  • AskApache


    Apache loads every time anyway@... so using it

  • John

    Heya - the Python example actually would prompt a 302 redirect

    In order to prompt a 301, note that you would need to utilize the HttpResponsePermanentRedirect class a la:

    from django import http
    def view(request):
    return http.HttpResponsePermanentRedirect ('')

    Reference: httpresponse-subclasses

  • OneDame

    Hey, thanks for this list. It really helped me out!

  • Stefanos

    Great post! I found it really useful. Any idea if the header redirect is seo fiendly. Aa far as I know the best one is the .htaccess one. Any advice on this issue.

  • Big B.

    Script "Ultimate PHP method" reported Parse error: syntax error, unexpected $end

  • Abraham NooB


    If I want to redirect all pages from my old website to the home page of my new website, would this be enough:

    RedirectMatch 301 (.*)
  • Jason

    Do you know how to successfully do this on an smf forum? I'm not able to sign in when using this method.

  • Pingback: SEO: Οδηγός αρχαρίων και βέλτιστες πρακτικές | CREATIVE IDEAS

Related Articles

My Online Tools
Popular Articles

Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman


It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | askapache

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service

↑ TOPMain