title: Django: How to redirect to an external URL?
body: <p>[Django 1.0.2]</p>
  
  <p>I have a view set up like this:</p>
  
  <pre><code>(r'^redirect/(?P&lt;object_id&gt;\d+)/(?P&lt;url&gt;.*)/$',
      'django.views.generic.simple.redirect_to',
      {'content_type': SiteType}, 'clickout'),
  </code></pre>
  
  <p>When I get the following URL, two different things happen on local development server and on remote mod_wsgi server:</p>
  
  <pre><code># GET
  "/redirect/2/http://www.example.com//"
  
  # Remote server ends up (note the missing slash in the end)
  "http://mydomain.com/example.com"
  
  # Development server works as expected
  "http://www.example.com/"
  </code></pre>
  
  <p>I have inspected the HTTP headers and it really tries the wrong URL:</p>
  
  <pre><code>"""HTTP/1.x 301 MOVED PERMANENTLY
  
  Date: Thu, 26 Feb 2009 06:58:35 GMT
  
  Server: Apache/2.0.52 (Red Hat) mod_wsgi/2.0 Python/2.5
  
  Etag: "d41d8cd98f00b204e9800998ecf8427e"
  
  Location: http://mydomain.com/example.com
  Content-Type: text/html; charset=utf-8
  
  Vary: User-Agent,Accept-Encoding
  
  Content-Encoding: gzip
  
  Content-Length: 20
  </code></pre>
  
  <p>"""</p>
  
  <p>What could be the problem? How can I redirect to an external URL?</p>
  
  <h2>Edit</h2>
  
  <p>It appears embedding URLs inside URLs is not a good idea. Both apache and mod_wsgi are collapsing double slashes. (<a href="http://groups.google.com/group/django-users/msg/76718270ffed9696?pli=1" rel="nofollow noreferrer">here is an explanation</a>)</p>
  
  <p>But giving the target URL as a parameter works fine:</p>
  
  <pre><code>"/redirect/2/?url=http://www.example.com/"
  </code></pre>
  
  <p>Thanks a lot Sean F from <a href="http://www.webfaction.com/" rel="nofollow noreferrer">Webfaction</a> support for helping me out.</p>