Rewrite /p/ on etherpad.

Also, actually redirect on incorrect hostnames.

Change-Id: I4e0981e45ed119eff02027e90a6a983ddeb6138b
Reviewed-on: https://review.openstack.org/14420
Reviewed-by: Monty Taylor <mordred@inaugust.com>
Reviewed-by: Clark Boylan <clark.boylan@gmail.com>
Reviewed-by: Paul Belanger <paul.belanger@polybeacon.com>
Approved: James E. Blair <corvus@inaugust.com>
Tested-by: Jenkins
diff --git a/templates/etherpadlite.vhost.erb b/templates/etherpadlite.vhost.erb
index 6ac0aec..e9802a5 100644
--- a/templates/etherpadlite.vhost.erb
+++ b/templates/etherpadlite.vhost.erb
@@ -35,9 +35,25 @@
 
   RewriteEngine on
   RewriteCond %{HTTP_HOST} !<%= scope.lookupvar("etherpad_lite::apache::vhost_name") %>
-  RewriteRule ^.*$ https://<%= scope.lookupvar("etherpad_lite::apache::vhost_name") %>
-  RewriteRule ^/(.*)$ http://localhost:9001/$1 [P]
+  RewriteRule ^.*$ https://<%= scope.lookupvar("etherpad_lite::apache::vhost_name") %> [L,R=301]
 
+  RewriteRule ^/p/(.*)$ https://<%= scope.lookupvar("etherpad_lite::apache::vhost_name") %>/$1 [NC,L,R=301]
+  RewriteCond %{REQUEST_URI} !^/p/
+  RewriteCond %{REQUEST_URI} !^/static/
+  RewriteCond %{REQUEST_URI} !^/pluginfw/
+  RewriteCond %{REQUEST_URI} !^/javascripts/
+  RewriteCond %{REQUEST_URI} !^/socket.io/
+  RewriteCond %{REQUEST_URI} !^/ep/
+  RewriteCond %{REQUEST_URI} !^/minified/
+  RewriteCond %{REQUEST_URI} !^/api/
+  RewriteCond %{REQUEST_URI} !^/ro/
+  RewriteCond %{REQUEST_URI} !^/error/
+  RewriteCond %{REQUEST_URI} !^/jserror
+  RewriteCond %{REQUEST_URI} !/favicon.ico
+  RewriteCond %{REQUEST_URI} !/robots.txt
+  RewriteRule ^/+(.+)$ https://<%= scope.lookupvar("etherpad_lite::apache::vhost_name") %>/p/$1
+
+  RewriteRule ^/(.*)$ http://localhost:9001/$1 [P]
   ProxyPassReverse / http://localhost:9001/
 
 </VirtualHost>