<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=Rewrites_and_Redirects</id>
	<title>Rewrites and Redirects - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=Rewrites_and_Redirects"/>
	<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=Rewrites_and_Redirects&amp;action=history"/>
	<updated>2026-05-13T09:38:49Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://training-course-material.com/index.php?title=Rewrites_and_Redirects&amp;diff=23942&amp;oldid=prev</id>
		<title>Cesar Chew at 17:24, 24 November 2014</title>
		<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=Rewrites_and_Redirects&amp;diff=23942&amp;oldid=prev"/>
		<updated>2014-11-24T17:24:47Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Cat|Nginx|03}}&lt;br /&gt;
{{Cat|Apache|05}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;slideshow style=&amp;quot;nobleprog&amp;quot; headingmark=&amp;quot;⌘&amp;quot; incmark=&amp;quot;…&amp;quot; scaled=&amp;quot;true&amp;quot; font=&amp;quot;Trebuchet MS&amp;quot; &amp;gt;&lt;br /&gt;
;title: Rewrites and Redirects&lt;br /&gt;
;author: Sam Bashton (NobleProg Ltd)&lt;br /&gt;
&amp;lt;/slideshow&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Redirects ⌘===&lt;br /&gt;
* Content moved, but still want it to be available at another location?&lt;br /&gt;
* Use a redirect&lt;br /&gt;
* Very very common, in part thanks to SEO&lt;br /&gt;
=== Simple redirect ⌘===&lt;br /&gt;
* page at `/calzado/` has been moved to `/zapatos/`&lt;br /&gt;
* We need to send a redirect telling all clients it has been permanently moved&lt;br /&gt;
=== 302: Found ⌘===&lt;br /&gt;
* Used when a document at one URL should in fact be requested via another&lt;br /&gt;
* Server sends back a redirect to the other location&lt;br /&gt;
* Browser makes two requests - one for the initial URL, one for the location given in the reply&lt;br /&gt;
* Browser should not cache this redirect&lt;br /&gt;
=== 301: Moved Permanently ⌘===&lt;br /&gt;
* Sends back a redirect to the browser with the new HTTP location&lt;br /&gt;
* Browser then makes a second request, to the location given in the reply to the first request&lt;br /&gt;
* Browser can cache this redirect&lt;br /&gt;
* Also obeyed by search engines (Google)&lt;br /&gt;
=== Our shoe redirect ⌘===&lt;br /&gt;
 ```&lt;br /&gt;
 location /calzado {&lt;br /&gt;
  rewrite ^ http://localhost:8080/zapatos;&lt;br /&gt;
 }&lt;br /&gt;
 ```&lt;br /&gt;
=== Testing our redirect ⌘===&lt;br /&gt;
We can easily test the redirect using `curl`&lt;br /&gt;
&lt;br /&gt;
 ```&lt;br /&gt;
 $ curl -I http://localhost/calzado/&lt;br /&gt;
 HTTP/1.1 302 Moved Temporarily&lt;br /&gt;
 Server: nginx/1.4.1&lt;br /&gt;
 Date: Tue, 28 May 2013 13:59:32 GMT&lt;br /&gt;
 Content-Type: text/html&lt;br /&gt;
 Content-Length: 160&lt;br /&gt;
 Connection: keep-alive&lt;br /&gt;
 Location: http://localhost:8080/zapatos/&lt;br /&gt;
 ```&lt;br /&gt;
=== Changing the 302 to a 301 ⌘===&lt;br /&gt;
 ```&lt;br /&gt;
 location /calzado {&lt;br /&gt;
  rewrite ^ http://localhost:8080/zapatos permanent;&lt;br /&gt;
 }&lt;br /&gt;
 ```&lt;br /&gt;
=== More complex redirects ⌘===&lt;br /&gt;
&lt;br /&gt;
* Now we need to redirect mens and womens shoes&lt;br /&gt;
* We can use both a regex location match and regex rewrite:&lt;br /&gt;
 ```&lt;br /&gt;
 location ~ /(hombre|mujer)/calzado/ {&lt;br /&gt;
  rewrite /(hombre|mujer)/calzado/ http://localhost:8080/$1/zapatos/;&lt;br /&gt;
 }&lt;br /&gt;
 ```&lt;br /&gt;
=== Test complex redirect ⌘===&lt;br /&gt;
 ```&lt;br /&gt;
 $ curl -I http://localhost/mujer/calzado/&lt;br /&gt;
 HTTP/1.1 302 Moved Temporarily&lt;br /&gt;
 Server: nginx/1.4.1&lt;br /&gt;
 Date: Tue, 28 May 2013 14:20:24 GMT&lt;br /&gt;
 Content-Type: text/html&lt;br /&gt;
 Content-Length: 160&lt;br /&gt;
 Connection: keep-alive&lt;br /&gt;
 Location: http://localhost:8080/mujer/zapatos/&lt;br /&gt;
 ```&lt;br /&gt;
=== Adding more complexity ⌘===&lt;br /&gt;
* We now want our redirect only for the host example.es&lt;br /&gt;
* We can add a conditional&lt;br /&gt;
 ```&lt;br /&gt;
 location ~ /(hombre|mujer)/calzado/ {&lt;br /&gt;
  if ($host = &amp;quot;example.es&amp;quot;) {&lt;br /&gt;
    rewrite /(hombre|mujer)/calzado/ http://localhost:8080/$1/zapatos/;&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 ```&lt;br /&gt;
=== Testing if ⌘===&lt;br /&gt;
 ```&lt;br /&gt;
 $ curl -I http://localhost/mujer/calzado/&lt;br /&gt;
 HTTP/1.1 404 Not Found&lt;br /&gt;
 Server: nginx/1.4.1&lt;br /&gt;
 Date: Tue, 28 May 2013 14:38:59 GMT&lt;br /&gt;
 Content-Type: text/html&lt;br /&gt;
 Content-Length: 168&lt;br /&gt;
 Connection: keep-alive&lt;br /&gt;
 $ curl -I http://example.es/mujer/calzado/&lt;br /&gt;
 HTTP/1.1 302 Moved Temporarily&lt;br /&gt;
 Server: nginx/1.4.1&lt;br /&gt;
 Date: Tue, 28 May 2013 14:39:04 GMT&lt;br /&gt;
 Content-Type: text/html&lt;br /&gt;
 Content-Length: 160&lt;br /&gt;
 Connection: keep-alive&lt;br /&gt;
 Location: http://localhost:8080/mujer/zapatos/&lt;br /&gt;
 ```&lt;br /&gt;
 === Rewrites ⌘===&lt;br /&gt;
* Much the same as redirects, but done server side&lt;br /&gt;
* No visibility to the end user &lt;br /&gt;
=== Rewrite example ===&lt;br /&gt;
* Requests for `/en/shoes/` should be fulfilled by the content in the&lt;br /&gt;
  `/zapatos/` directory&lt;br /&gt;
 ```&lt;br /&gt;
 location / {&lt;br /&gt;
  root /usr/share/nginx/html;&lt;br /&gt;
  index index.html index.html;&lt;br /&gt;
  rewrite /en/shoes/ /zapatos/;&lt;br /&gt;
 }&lt;br /&gt;
 ```&lt;br /&gt;
=== try_files ⌘===&lt;br /&gt;
* One common pattern is for rewrites to be used only if a file&lt;br /&gt;
  of the corresponding name doesn&amp;#039;t exist on disk&lt;br /&gt;
* For example, everything should be handled by index.php, with the path&lt;br /&gt;
  passed as a query string&lt;br /&gt;
 ```&lt;br /&gt;
location / {&lt;br /&gt;
   root /usr/share/nginx/html;&lt;br /&gt;
  index index.php;&lt;br /&gt;
  try_files $uri $uri/ /index.php?q=$uri;&lt;br /&gt;
 }&lt;br /&gt;
 ```&lt;br /&gt;
&lt;br /&gt;
=== Exercise ⌘===&lt;br /&gt;
* Implement serving of /extra/ from /usr/share/doc/HTML using a rewrite&lt;br /&gt;
* Make requests to /foo/ send a permanent redirect to /extra/&lt;/div&gt;</summary>
		<author><name>Cesar Chew</name></author>
	</entry>
</feed>