Index: src/main/java/com/xpn/xwiki/XWiki.java
===================================================================
--- src/main/java/com/xpn/xwiki/XWiki.java	(revision 12955)
+++ src/main/java/com/xpn/xwiki/XWiki.java	(working copy)
@@ -387,15 +387,32 @@
             String host = "";
             try {
                 URL requestURL = context.getURL();
+                System.out.println("url: " + requestURL.toString());
                 host = requestURL.getHost();
             } catch (Exception e) {
             }
 
+            // we will use an element of the path to get the host
+            // This only happens if the url is in the form contains /xwiki/wiki_wikiname/
+            if ("1".equals(xwiki.Param("xwiki.virtual.usepath", "0"))) {
+                String uri = request.getRequestURI();
+                System.out.println("uri: " + uri);
+                int vhi1 = uri.indexOf("/", 1);
+                int vhi2 = uri.indexOf("/", vhi1 + 1);
+                int vhi3 = uri.indexOf("/", vhi2 + 1);
+                String subwiki = uri.substring(vhi1+1, vhi2);
+                String virtualHost = uri.substring(vhi2+1, vhi3);
+                if (subwiki.equals("subwiki"))
+                    host = virtualHost.substring(5) + "." + host;
+            }
+
             if (host.equals("")) {
                 return xwiki;
             }
 
+            System.out.println("host: " + host);
             String appname = xwiki.findWikiServer(host, context);
+            System.out.println("appname: " + appname);
 
             if (appname == null) {
                 String uri = request.getRequestURI();
@@ -413,7 +430,7 @@
                 if ("0".equals(xwiki.Param("xwiki.virtual.autowww"))) {
                     appname = servername;
                 } else {
-                    // As a convenience, allow sites starting with www, localhost or using an
+                  // As a convenience, allow sites starting with www, localhost or using an
                     // IP address not to have to create a XWikiServerXwiki page since we consider
                     // in that case that they're pointing to the main wiki.
                     if ((servername.equals("www"))
@@ -4188,6 +4205,12 @@
     public URL getServerURL(String database, XWikiContext context) throws MalformedURLException
     {
         String serverurl = null;
+
+        // In virtual wiki path mode the server is the standard one
+        if ("1".equals(Param("xwiki.virtual.usepath", "0"))) {
+           return null;
+        }
+        
         if (database != null) {
             String db = context.getDatabase();
             try {
@@ -4229,6 +4252,38 @@
         }
     }
 
+    public String getServerPath(String database, XWikiContext context)
+    {
+        // unless we are in virtual wiki path mode we should return null
+        if ("0".equals(Param("xwiki.virtual.usepath", "0"))) {
+            return "";
+        }
+
+        if (database != null) {
+            String db = context.getDatabase();
+            try {
+                context.setDatabase(getDatabase());
+                XWikiDocument doc =
+                        getDocument("XWiki.XWikiServer" + database.substring(0, 1).toUpperCase() + database.substring(1),
+                                context);
+                BaseObject serverobject = doc.getObject("XWiki.XWikiServerClass");
+                if (serverobject != null) {
+                    String server = serverobject.getStringValue("server");
+                    if (server != null) {
+                        server = server.substring(0, server.indexOf("."));
+                        return "subwiki/wiki_" + server + "/";
+                    }
+                }
+            } catch (Exception ex) {
+                ex.printStackTrace();
+            } finally {
+                context.setDatabase(db);
+            }
+        }
+
+        return "";
+    }
+
     public String getURL(String fullname, String action, XWikiContext context) throws XWikiException
     {
         XWikiDocument doc = new XWikiDocument();
@@ -4390,9 +4445,14 @@
                     // potential issue with i18n encoding as stated above).
                     path = request.getPathInfo();
                 } else {
+                    // we need to get rid of /xwiki/bin/ or /xwiki/subwiki/wiki_wikiname/ in case of a XEM in usepath mode
+                    String serverPath = getServerPath(context.getDatabase(), context);
+                    if (serverPath.equals(""))
+                        serverPath = request.getServletPath();
+
                     path =
-                        request.getRequestURI().substring(
-                            request.getContextPath().length() + request.getServletPath().length());
+                            request.getRequestURI().substring(
+                                    request.getContextPath().length() + serverPath.length());
                 }
 
                 // Fix error in some containers, which don't hide the jsessionid parameter from the
Index: src/main/java/com/xpn/xwiki/web/XWikiServletURLFactory.java
===================================================================
--- src/main/java/com/xpn/xwiki/web/XWikiServletURLFactory.java	(revision 12955)
+++ src/main/java/com/xpn/xwiki/web/XWikiServletURLFactory.java	(working copy)
@@ -48,6 +48,8 @@
 
     protected String servletPath;
 
+    protected String multiwikiPath;
+
     public XWikiServletURLFactory()
     {
     }
@@ -194,7 +196,7 @@
         }
 
         StringBuffer newpath = new StringBuffer(contextPath);
-        newpath.append(servletPath);
+        addServletPath(newpath, xwikidb, context);
 
         addAction(newpath, action, context);
         addSpace(newpath, web, action, context);
@@ -219,6 +221,31 @@
         }
     }
 
+    private void addServletPath(StringBuffer newpath, String xwikidb, XWikiContext context)
+    {
+        String spath = "";
+
+        if (xwikidb==null)
+            xwikidb = context.getDatabase();
+
+        System.out.println("Making url in db " + xwikidb);
+        // In virtual wiki mode we need to add the virtual wiki name in the URL instead of bin
+        if (!xwikidb.equals(context.getWiki().getDatabase())) {
+            System.out.println("Making url in db 2  " + xwikidb);
+                if ("1".equals(context.getWiki().Param("xwiki.virtual.usepath", "0"))) {
+                System.out.println("Making url in db 3  " + xwikidb);
+                     spath = context.getWiki().getServerPath(xwikidb, context);
+            }
+        }
+
+        System.out.println("spath: " + spath);
+
+        if (spath.equals(""))
+            newpath.append(servletPath);
+        else
+            newpath.append(spath);
+    }
+
     private void addAction(StringBuffer newpath, String action, XWikiContext context)
     {
         boolean showViewAction = context.getWiki().showViewAction(context);
@@ -294,7 +321,7 @@
         XWikiContext context)
     {
         StringBuffer newpath = new StringBuffer(contextPath);
-        newpath.append(servletPath);
+        addServletPath(newpath, xwikidb, context);
         addAction(newpath, "skin", context);
         addSpace(newpath, web, "skin", context);
         addName(newpath, name, "skin", context);
@@ -344,7 +371,7 @@
         }
 
         StringBuffer newpath = new StringBuffer(contextPath);
-        newpath.append(servletPath);
+        addServletPath(newpath, xwikidb, context);
         addAction(newpath, action, context);
         addSpace(newpath, web, action, context);
         addName(newpath, name, action, context);
@@ -374,7 +401,7 @@
     {
         String action = "downloadrev";
         StringBuffer newpath = new StringBuffer(contextPath);
-        newpath.append(servletPath);
+        addServletPath(newpath, xwikidb, context);
         addAction(newpath, action, context);
         addSpace(newpath, web, action, context);
         addName(newpath, name, action, context);
Index: src/main/java/com/xpn/xwiki/web/XWikiRequestProcessor.java
===================================================================
--- src/main/java/com/xpn/xwiki/web/XWikiRequestProcessor.java	(revision 12955)
+++ src/main/java/com/xpn/xwiki/web/XWikiRequestProcessor.java	(working copy)
@@ -34,6 +34,13 @@
         HttpServletResponse httpServletResponse) throws IOException
     {
         String result = super.processPath(httpServletRequest, httpServletResponse);
+
+        System.out.println("Path: " + result);
+        if (result.startsWith("/wiki_")) {
+          result = result.substring(result.indexOf("/", 1));           
+        }
+
+        System.out.println("Path2: " + result);
         if (StringUtils.countMatches(result, "/") <= 2) {
             if (result.startsWith("/xmlrpc/")) {
                 return "/xmlrpc/";
