« Simple PHP Anti-Spam Captcha ScriptBest Free Online Banking for Web Professionals »
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!
Am I missing something? Most definately! Please use the comment form below to suggest new code.
These go in the <head> </head> section of your html. Usually when you use this meta redirect method you should also use the javascript method, just to be safe.
Redirects to http://www.askapache.com after 0 seconds.
<meta http-equiv="refresh" content="0;url=http://www.askapache.com" />
These go in the <head> </head> section of your html.
Will redirect user to http://www.askapache.com immediately
<script type="text/javascript"> window.location.href='http://www.askapache.com'; </script>
This will redirect to http://www.askapache.com after 2 seconds
<body onload="javascript:setTimeout(function(){window.location.href='http://www.askapache.com'},2000);">
<?php
header('Location:http://www.askapache.com');
exit;
exit();
?>
<?php
header('Refresh: 0; URL=http://www.askapache.com');
exit;
exit();
?>
It redirects to a page specified by “$url”. $mode can be:
<?php
function do_redirect($url,$mode)
{
if (strncmp('http:',$url,5) && strncmp('https:',$url,6)) {
$starturl = ($_SERVER["HTTPS"] == 'on' ? 'https' : 'http') . '://'.
(empty($_SERVER['HTTP_HOST'])? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST']);
if ($url[0] != '/') $starturl .= dirname($_SERVER['PHP_SELF']).'/';
$url = "$starturl$url";
}
switch($mode) {
case 'LOCATION':
header("Location: $url");
exit;
case 'REFRESH':
header("Refresh: 0; URL=\"$url\"");
exit;
case 'META':
exit;
default:
?><script type="text/javascript">
window.location.href='<?=$url?>';
</script><?
}
exit;
}
?>
#!/usr/bin/perl my $URL = "http://www.askapache.com"; print "Status: 302 Moved\nLocation: $URL\n\n";
#!/usr/bin/perl print "Location: http://www.askapache.com\n\n"; exit;
<cfheader statuscode="301" statustext="Moved Permanently"> <cfheader name="Location" value="http://www.askapache.com"> <cfabort>
<cflocation url="http://www.askapache.com">
<%@ Language=VBScript %> <% response.status="301 moved permanently" Response.AddHeader "Location", "http://www.askapache.com" %>
#!/bin/sh echo "Content-type: text/html" echo "Location: http://www.askapache.com"; echo ""; exit 0;
See also Ultimate Apache htaccess article and URL Redirection
Redirects permanently when a request is made to site.com/thisword.html to http://www.askapache.com
RewriteEngine On RewriteBase / RewriteRule ^thisword\.html$ http://www.askapache.com [R=301,NC,L]
Redirects temporarily when a request is made to site.com/thisword.html to http://www.askapache.com
RewriteEngine On RewriteBase / RewriteRule ^thisword\.html$ http://www.askapache.com [R,NC,L]
Redirect 301 /thisword.html http://www.askapache.com
RedirectPerm /thisword.html http://www.askapache.com
RedirectMatch 301 ^thisword\.html$ http://www.askapache.com
Redirect 302 /thisword.html http://www.askapache.com
RedirectTemp /thisword.html http://www.askapache.com
RedirectMatch 302 ^thisword\.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 one.
from django import http
def view(request):
return http.HttpResponseRedirect('http://www.askapache.com')
« Simple PHP Anti-Spam Captcha Script
Best Free Online Banking for Web Professionals »
Tags: ASP, ColdFusion, htaccess, HTTP-EQUIV, Javascript, Perl, PHP, Python
Please consider donating to support active development of the free software and articles here.![]()
The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. Tim Berners-Lee
This is great information. Thanks a bunch
Excellent collection! Thx
Thanks for such nice list
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(‘http://www.htaccesselite.com’)
Thanks for this fantastic checklist! I will send my students to this page.
It's very simple - you read the protocol and write the code. -Bill Joy
HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C | TLDP | WAI | DISA | ICSI | GIAC | SANS RR | GHOST | DEFCON | NIST | DHS CYBER | NIST | .:: Phrack Magazine ::.
↑ TOPExcept 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. HTTPD based on NCSA HTTPd
Which would be the preferred redirect method: PHP or Mod Rewrite ? Why?
I’m just curious abut it.