diff --git a/lib/SimpleSAML/XHTML/Template.php b/lib/SimpleSAML/XHTML/Template.php
index 6956e1fd87f7fef3901e552d03d754d3878d1844..747c9b537dafea0b53e8554ee7e4ba4495a78bc6 100644
--- a/lib/SimpleSAML/XHTML/Template.php
+++ b/lib/SimpleSAML/XHTML/Template.php
@@ -54,13 +54,27 @@ class SimpleSAML_XHTML_Template
         $this->useTwig =  $this->setupTwig();
     }
 
-    private function setupTwigTemplatepaths()
+    /*
+     * Normalize template-name
+     * *param $templateName         Template
+     */
+    private function normalizeTemplateName($templateName)
     {
-        $filename = $this->template;
-        // normalize template name
-        if (strripos($filename, '.php', 0)) {
-            $filename = basename(basename($filename, '.tpl.php'), '.php').'.twig.html';
+        if (strripos($templateName, '.twig.html')) { return $templateName; }
+        $phppos = strripos($templateName, '.php');
+        if ($phppos) {
+            $templateName = substr($templateName, 0, $phppos);
+        }
+        $tplpos = strripos($templateName, '.tpl');
+        if ($tplpos) {
+            $templateName = substr($templateName, 0, $tplpos);
         }
+        return $templateName.'.twig.html';
+    }
+
+    private function setupTwigTemplatepaths()
+    {
+        $filename = $this->normalizeTemplateName($this->template);
         // get namespace if any
         $namespace = '';
         $split = explode(':', $filename, 2);