Index: mapwms.c
===================================================================
--- mapwms.c	(revision 7141)
+++ mapwms.c	(working copy)
@@ -425,11 +425,26 @@
       for(j=0; j<map->numlayers; j++)
       {
         /* Keep only layers with status=DEFAULT by default */
-        /* Layer with status DEFAULT is drawn first. */
+        /* Layer with status DEFAULT is drawn first, unless specified in the
+        /* LAYERS parameter. */
         if (GET_LAYER(map, j)->status != MS_DEFAULT)
            GET_LAYER(map, j)->status = MS_OFF;
-        else
-           map->layerorder[nLayerOrder++] = j;
+        else {
+          /* If this layer (of type MS_DEFAULT) is included in the layers list,  */
+          /* don't set its layerorder yet, it will be done below. Otherwise draw */
+          /* this layer first */
+          layerfound = 0;
+          for (k=0; k<numlayers; k++) {
+            if (strcasecmp(GET_LAYER(map, j)->name, layers[k]) == 0) {
+              /* Layer is referenced in the LAYERS list, don't set it now */
+              layerfound = 1;
+              break;
+            }
+          }
+          
+          if (!layerfound)
+            map->layerorder[nLayerOrder++] = j;
+        }
       }
 
       for (k=0; k<numlayers; k++)
@@ -449,9 +464,10 @@
                   layerfound = 1;
               }
           }
-          if (layerfound == 0)
+          if (layerfound == 0) {
+            msDebug("msWMSLoadGetMapParams(): Unknown layer '%s' given in LAYERS parameter\n", layers[k]);
             invalidlayers++;
-
+          }
       }
 
       /* set all layers with status off at end of array */
