<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: redirect code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 03:03:39 GMT</pubDate>
    <description>DZone Snippets: redirect code</description>
    <item>
      <title>ASP.NET Permanent Redirect (301)</title>
      <link>http://snippets.dzone.com/posts/show/5818</link>
      <description>Response.Redirect normally returns a 302 Object moved HTTP response. However a 301 permanent redirect is often better for search engine spiders:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Response.StatusCode = 301;&lt;br /&gt;Response.AddHeader("Location", "New.aspx");&lt;br /&gt;Response.Write("&lt;html&gt;&lt;body&gt;Page moved permanently to &lt;a href="New.aspx"&gt;New&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;");&lt;br /&gt;Response.End();&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 24 Jul 2008 01:36:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5818</guid>
      <author>ixnay2infinity (AvG)</author>
    </item>
    <item>
      <title>Using Bluetooth to control Asterisk calls.</title>
      <link>http://snippets.dzone.com/posts/show/5660</link>
      <description>Redirect an Asterisk call&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    de61.old_status = get_status(de61.file) # found is either true or false&lt;br /&gt;    de61.old_location = get_location(de61.file) # location is either 'at home', 'gone to bed' or 'asleep'&lt;br /&gt;    &lt;br /&gt;    while count &lt; 1&lt;br /&gt;&lt;br /&gt;      puts 'old status : ' + de61.old_status.to_s&lt;br /&gt;      de61.new_status = check_device(de61.id, de61.name)&lt;br /&gt;      puts 'new_status: ' + de61.new_status.to_s&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;      if de61.new_status and de61.old_status&lt;br /&gt;        if de61.old_status == false&lt;br /&gt;          puts 'connecting to the bluetooth device ...'&lt;br /&gt;          Thread.new{`rfcomm connect 0 #{de61.id}`}&lt;br /&gt;          sleep 12&lt;br /&gt;        end&lt;br /&gt;        de61.new_location = check_location(de61.id) &lt;br /&gt;      end&lt;br /&gt;      &lt;br /&gt;      puts 'old_location: ' + de61.old_location.to_s&lt;br /&gt;      puts 'new_location: ' + de61.new_location.to_s&lt;br /&gt;      update_status(de61.project, de61.new_status, de61.new_location) if ((de61.old_status != de61.new_status.to_s) or (de61.old_location != de61.new_location)) &lt;br /&gt;        &lt;br /&gt;      mystatus = ''&lt;br /&gt;      &lt;br /&gt;      if  (not de61.old_status == de61.new_status) then&lt;br /&gt;        if (de61.new_status.to_s == 'true') then&lt;br /&gt;            mystatus = ' has entered the building'&lt;br /&gt;            callto('SIP/line1')&lt;br /&gt;        elsif  (de61.new_status == false) then&lt;br /&gt;          mystatus = ' has left the building'&lt;br /&gt;          callto('SIP/07736668666@sipgate')&lt;br /&gt;        end&lt;br /&gt;        puts 'updating in or out the building'&lt;br /&gt;        ms = MyStatus.new(mystatus)&lt;br /&gt;      elsif (de61.old_location.to_s != de61.new_location.to_s)&lt;br /&gt;        if de61.old_location.match(/sleep/) and de61.new_location.match(/home|bed/) and not old_mystatus.match(/awoken/) then&lt;br /&gt;          mystatus = ' has awoken'&lt;br /&gt;        elsif (de61.old_location.match(/home|bed/)) and de61.new_location.match(/sleep/) &lt;br /&gt;          mystatus = ' has gone to sleep'           &lt;br /&gt;        elsif (de61.old_location.match(/home/)) and de61.new_location.match(/bed/) &lt;br /&gt;          mystatus = ' has gone to bed'&lt;br /&gt;        elsif de61.old_location.match(/bed/) and de61.new_location.match(/home/) &lt;br /&gt;          mystatus = ' has got out of bed'&lt;br /&gt;        end&lt;br /&gt;        puts 'updating home location'&lt;br /&gt;        ms = MyStatus.new(mystatus) if mystatus != ''&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;      de61.old_location = de61.new_location&lt;br /&gt;      de61.new_location = 'at home'&lt;br /&gt;      de61.old_status = de61.new_status&lt;br /&gt;      old_mystatus = mystatus&lt;br /&gt;      sleep 7&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;    &lt;br /&gt;  def callto(dialstring)&lt;br /&gt;    file = File.new('/etc/asterisk/ruby/call-to.txt','w')&lt;br /&gt;    file.puts 'Dial(' + dialstring + ')'&lt;br /&gt;    file.close&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;file: extensions.conf &lt;br /&gt;&lt;code&gt;&lt;br /&gt;exten =&gt; 5168666,n,ReadFile(dialplan1=/etc/asterisk/ruby/call-to.txt,130)&lt;br /&gt;exten =&gt; 5168666,n,Exec(${dialplan1})&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 17 Jun 2008 20:52:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5660</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Redirect a URL with Ruby CGI</title>
      <link>http://snippets.dzone.com/posts/show/5415</link>
      <description>&lt;code&gt;&lt;br /&gt;#!/usr/bin/ruby&lt;br /&gt;&lt;br /&gt;require 'cgi'&lt;br /&gt;&lt;br /&gt;cgi = CGI.new&lt;br /&gt;print cgi.header({'Status' =&gt; '302 Moved', 'location' =&gt;  'http://www.wired.com'})&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;or&lt;br /&gt;&lt;code&gt;&lt;br /&gt;url = 'http://www.wired.com/'&lt;br /&gt;print cgi.header({'status'=&gt;'REDIRECT', 'Location'=&gt;url})&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;References:&lt;br /&gt;&lt;a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/48889"&gt;RE: cgi redirect&lt;/a&gt; [nagaokaut.ac.jp]&lt;br /&gt;&lt;a href="http://www.mokehehe.com/assari/index.php?Ruby%2FCGI"&gt;Ruby/CGI - assari&lt;/a&gt; [mokehehe.com]&lt;br /&gt;&lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html"&gt;HTTP/1.1: Status Code Definitions&lt;/a&gt; [w3.org]</description>
      <pubDate>Mon, 21 Apr 2008 12:25:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5415</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>redirect a user to a new page</title>
      <link>http://snippets.dzone.com/posts/show/4820</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// insert code here..&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 28 Nov 2007 15:43:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4820</guid>
      <author>chrisbud (Christine Budzowski)</author>
    </item>
    <item>
      <title>Rails: 301 Permanent Redirect</title>
      <link>http://snippets.dzone.com/posts/show/4779</link>
      <description>First there was:&lt;br /&gt;&lt;code&gt;&lt;br /&gt; headers["Status"] = "301 Moved Permanently"  &lt;br /&gt; redirect_to "http://www.newdomain.com"  &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Then there was:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;head :moved_permanently, :location =&gt; book_url(@book)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;As of [7820] on 2007/10/09, there's a new redirector in town.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  # Examples: &lt;br /&gt;  #   redirect_to post_url(@post), :status=&gt;:found &lt;br /&gt;  #   redirect_to :action=&gt;'atom', :status=&gt;:moved_permanently &lt;br /&gt;  #   redirect_to post_url(@post), :status=&gt;301 &lt;br /&gt;  #   redirect_to :action=&gt;'atom', :status=&gt;302&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;sweet.&lt;br /&gt;&lt;br /&gt;see: http://dev.rubyonrails.org/changeset/7820</description>
      <pubDate>Fri, 16 Nov 2007 06:17:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4779</guid>
      <author>MattScilipoti (Matt Scilipoti)</author>
    </item>
    <item>
      <title>Response::redirect //PHP Function</title>
      <link>http://snippets.dzone.com/posts/show/2449</link>
      <description>&lt;code&gt;&lt;br /&gt;class Response{&lt;br /&gt;	function redirect($url){&lt;br /&gt;		exit(header('Location: ' . $url));&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 20 Aug 2006 21:28:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2449</guid>
      <author>jonasraoni (Jonas Raoni Soares Silva)</author>
    </item>
    <item>
      <title>HTML Redirect</title>
      <link>http://snippets.dzone.com/posts/show/2074</link>
      <description>I know this is a lame snippet, but I'm tired of writing this from scratch every time.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;html&gt;&lt;br /&gt;&lt;head&gt;&lt;br /&gt;  &lt;title&gt;Page Title&lt;/title&gt;&lt;br /&gt;  &lt;meta http-equiv="Refresh" content="2; URL=http://the.url"&gt;&lt;br /&gt;  &lt;script type="text/javascript"&gt;&lt;br /&gt;    location.replace('http://the.url');&lt;br /&gt;  &lt;/script&gt;&lt;br /&gt;&lt;/head&gt;&lt;br /&gt;&lt;body&gt;&lt;br /&gt;  &lt;p&gt;&lt;a href="http://the.url"&gt;Page Title&lt;/a&gt;&lt;/p&gt;&lt;br /&gt;&lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 18 May 2006 20:43:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2074</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Java Script Redirect</title>
      <link>http://snippets.dzone.com/posts/show/1832</link>
      <description>&lt;code&gt;&lt;script language="JavaScript"&gt;&lt;br /&gt;&lt;!--&lt;br /&gt;window.location="http://www.anotherpage.com/";&lt;br /&gt;// --&gt;&lt;br /&gt;&lt;/script&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Alternately, something like..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;a href="#" onclick="window.location='http://www.something.com/';" return false;"&gt;Click here&lt;/a&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;which stops it from being indexed (although rel="noindex" does the same on some engines).</description>
      <pubDate>Mon, 03 Apr 2006 16:18:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1832</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Performing a redirect with AJAX</title>
      <link>http://snippets.dzone.com/posts/show/1156</link>
      <description>If a redirect is returned from the rails' form_remote_tag or link_to_remote methods, a message is displayed saying something like "You are being redirected", where "redirected" is a link the user must click.  The code snippet below will actually redirect the user to the proper location, without them having to click the link.  Code works for both form_remote_tag or link_to_remote methods.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% link_to_remote "Foo",&lt;br /&gt;                  :url =&gt; { :action =&gt; "foo" },&lt;br /&gt;                  302 =&gt; "document.location = request.getResponseHeader('location')" %&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Or, an example from the Typo source:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;%= form_remote_tag :url =&gt; {:action =&gt; "comment", :id =&gt; @article}, &lt;br /&gt;                    :update =&gt; {:success =&gt; 'commentList'},&lt;br /&gt;                    :loading =&gt; "loading()", &lt;br /&gt;                    :complete =&gt; "complete(request)",&lt;br /&gt;                    :failure =&gt; "failure(request)",&lt;br /&gt;                    :html =&gt; {:id=&gt;"commentform",:class=&gt;"commentform"},&lt;br /&gt;	            302 =&gt; "document.location=request.getResponseHeader('location')"%&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 14 Jan 2006 02:55:30 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1156</guid>
      <author>brianellin (Brian Ellin)</author>
    </item>
    <item>
      <title>Redirect back to a previous action</title>
      <link>http://snippets.dzone.com/posts/show/1052</link>
      <description>I am pasting this verbatim from LoginSystem&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# move to the last store_location call or to the passed default one&lt;br /&gt;  def redirect_back_or_default(default)&lt;br /&gt;    if @session[:return_to].nil?&lt;br /&gt;      redirect_to default&lt;br /&gt;    else&lt;br /&gt;      redirect_to_url @session[:return_to]&lt;br /&gt;      @session[:return_to] = nil&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;In your controller...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt; redirect_back_or_default :controller =&gt; "myfuel", :action =&gt; "index"&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 04 Jan 2006 18:08:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1052</guid>
      <author>pmark (P. Mark Anderson)</author>
    </item>
  </channel>
</rss>
