I 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
- Redirect with meta http-equiv tags
- Redirect with PHP
- Redirect with Perl
- Redirect with ColdFusion (CFM)
- Redirect with ASP (VB Script)
- Redirect with shell script
- Redirect with mod_rewrite in apache htaccess
- Redirect with mod_alias in apache htaccess
Am I missing something? Most definately! Please use the comment form below to suggest new code.
These go in the
Redirects to http://www.askapache.com after 0 seconds.
These go in the
section of your html.
Will redirect user to http://www.askapache.com immediately
This will redirect to http://www.askapache.com after 2 seconds
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
#!/usr/bin/env perl my $URL = "http://www.askapache.com"; print "Status: 301 Moved\r\nLocation: $URL\r\n";
#!/usr/bin/env perl print "Status: 301 Moved\r\nLocation: http://www.askapache.com\r\n"; exit;
<%@ Language=VBScript %> <% response.status="301 moved permanently" Response.AddHeader "Location", "http://www.askapache.com" %>
#!/bin/sh -p echo -e "Status: 301 Moved Permanently\r\nLocation: http://www.askapache.com\r\n" echo -e "Content-type: text/html\r\n" exit 0;
Redirects permanently when a request is made to domain.tld/301-redirect.html to http://www.askapache.com
RewriteEngine On RewriteBase / RewriteRule ^301-redirect\.html$ http://www.askapache.com [R=301,NC,L]
Redirects temporarily when a request is made to domain.tld/301-redirect.html to http://www.askapache.com
RewriteEngine On RewriteBase / RewriteRule ^301-redirect\.html$ http://www.askapache.com [R,NC,L]
Redirect 301 /301-redirect.html http://www.askapache.com
RedirectPerm /301-redirect.html http://www.askapache.com
RedirectMatch 301 ^301-redirect\.html$ http://www.askapache.com
Redirect 302 /301-redirect.html http://www.askapache.com
RedirectTemp /301-redirect.html http://www.askapache.com
RedirectMatch 302 ^301-redirect\.html$ http://www.askapache.com
Issues a 302 Redirect to http://www.askapache.com when a file is not found. See also Force Apache to output any HTTP Status Code with ErrorDocument
ErrorDocument 404 http://www.askapache.com
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).
- Use standard redirects: don't break the back button!
- About redirects :
- The Top Ten New Mistakes of Web Design, by Jakob Nielsen, offers a view of why refresh should not be used for redirecting a document to a new address, as well as other (do's and) don't's on web design.
- The section 10.3 Redirection 3xx in Hypertext Transfer Protocol -- HTTP/1.1, is the authoritative documentation on HTTP redirects.
- How to do redirects with your server:
November 29th, 2007