diff --git a/doku.php b/doku.php
index d1781a9a2ee09331e3b3333f438f41fe94294b55..c69462574fccb6da7614e30c83dc4157e1d27e6a 100644
--- a/doku.php
+++ b/doku.php
@@ -5,14 +5,15 @@
  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
  * @author     Andreas Gohr <andi@splitbrain.org>
  */
-  
-  require_once("conf/dokuwiki.php");
-  require_once("inc/common.php");
-  require_once("inc/html.php");
-  require_once("inc/parser.php");
-  require_once("lang/en/lang.php");
-  require_once("lang/".$conf['lang']."/lang.php");
-  require_once("inc/auth.php");
+
+  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__)).'/');
+  require_once(DOKU_INC.'inc/init.php');
+  require_once(DOKU_INC.'inc/common.php');
+  require_once(DOKU_INC.'inc/html.php');
+  require_once(DOKU_INC.'inc/parser.php');
+  require_once(DOKU_INC.'lang/en/lang.php');
+  require_once(DOKU_INC.'lang/'.$conf['lang'].'/lang.php');
+  require_once(DOKU_INC.'inc/auth.php');
 
   //import variables
   $QUERY = trim($_REQUEST['id']);
@@ -73,7 +74,7 @@
       //unlock it
       unlock($id);
       //show it
-      header("Location: ".wl($ID, '','doku.php',true));
+      header("Location: ".wl($ID,'',true));
       exit();
     }
   }
diff --git a/feed.php b/feed.php
index 19d6729c20c578f3634f2ed3771062ef20c0e16b..d949345a94a31af85adf1233f454700e0d929ebb 100644
--- a/feed.php
+++ b/feed.php
@@ -6,7 +6,8 @@
  * @author     Andreas Gohr <andi@splitbrain.org>
  */
 
-  ini_set('short_open_tag',"1");
+  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__)).'/');
+  require_once(DOKU_INC.'inc/init.php');
   require_once("inc/common.php");
   require_once("inc/parser.php");
   require_once("inc/feedcreator.class.php");
@@ -42,15 +43,15 @@
   //some defaults for the feed
   $CACHEGROUP = 'feed';
   $conf['typography'] = false;
-  $conf['canonical']  = true;
+#  $conf['canonical']  = true;
   $parser['toc']      = false;
 
   $rss = new UniversalFeedCreator();
   $rss = new DokuWikiFeedCreator();
   $rss->title = $conf['title'];
-  $rss->link  = wl();
-  $rss->syndicationURL = getBaseURL().'/feed.php';
-  $rss->cssStyleSheet = getBaseURL().'/feed.css';
+  $rss->link  = DOKU_URL;
+  $rss->syndicationURL = DOKU_URL.'/feed.php';
+  $rss->cssStyleSheet  = DOKU_URL.'/feed.css';
 
   if($mode == 'list'){
     rssListNamespace($rss,$ns);
@@ -77,7 +78,7 @@ function rssRecentChanges(&$rss,$num){
     }
     $item = new FeedItem();
     $item->title       = $id;
-    $item->link        = wl($id,'rev='.$recents[$id]['date']);
+    $item->link        = wl($id,'rev='.$recents[$id]['date'],true);
     $item->description = $desc;
     $item->date        = date('r',$recents[$id]['date']);
     if(strpos($id,':')!==false){
@@ -115,7 +116,7 @@ function rssListNamespace(&$rss,$ns){
     $desc = cleanDesc(parsedWiki($id));
     $item = new FeedItem();
     $item->title       = $id;
-    $item->link        = wl($id,'rev='.$date);
+    $item->link        = wl($id,'rev='.$date,true);
     $item->description = $desc;
     $item->date        = date('r',$date);
     $rss->addItem($item);
diff --git a/inc/auth.php b/inc/auth.php
index 0266899a843d68c18bfdec50f3c86958db8af503..0943193779c2e58252ac1d6fa706ef3af7ae2489 100644
--- a/inc/auth.php
+++ b/inc/auth.php
@@ -1,4 +1,4 @@
-<?
+<?php
 /**
  * Authentication library
  *
@@ -9,12 +9,13 @@
  * @author     Andreas Gohr <andi@splitbrain.org>
  */
 
-  require_once("inc/common.php");
-  require_once("inc/io.php");
-  require_once("inc/blowfish.php");
-  require_once("inc/mail.php");
+  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
+  require_once(DOKU_INC.'inc/common.php');
+  require_once(DOKU_INC.'inc/io.php');
+  require_once(DOKU_INC.'inc/blowfish.php');
+  require_once(DOKU_INC.'inc/mail.php');
   // load the the auth functions
-  require_once('inc/auth_'.$conf['authtype'].'.php');
+  require_once(DOKU_INC.'inc/auth_'.$conf['authtype'].'.php');
 
   // some ACL level defines
   define('AUTH_NONE',0);
@@ -324,7 +325,7 @@ function auth_sendPassword($user,$password){
   if(!$userinfo['mail']) return false;
 
   $text = rawLocale('password');
-  $text = str_replace('@DOKUWIKIURL@',getBaseURL(true),$text);
+  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
   $text = str_replace('@FULLNAME@',$userinfo['name'],$text);
   $text = str_replace('@LOGIN@',$user,$text);
   $text = str_replace('@PASSWORD@',$password,$text);
diff --git a/inc/common.php b/inc/common.php
index 2e146d48505c524591659fc19da9eb5eed70dde1..24e9583f39dde6c64c38d9eafb153ccf3444912f 100644
--- a/inc/common.php
+++ b/inc/common.php
@@ -1,4 +1,4 @@
-<?
+<?php
 /**
  * Common DokuWiki functions
  *
@@ -6,100 +6,11 @@
  * @author     Andreas Gohr <andi@splitbrain.org>
  */
 
-  require_once("conf/dokuwiki.php");
-  require_once("inc/io.php");
-  require_once('inc/utf8.php');
-  require_once('inc/mail.php');
-
-  //set up error reporting to sane values
-  error_reporting(E_ALL ^ E_NOTICE);
-
-  //make session rewrites XHTML compliant
-  ini_set('arg_separator.output', '&amp;');
-
-  //init session
-  session_name("DokuWiki");
-  session_start();
-
-  //kill magic quotes
-  if (get_magic_quotes_gpc()) {
-    if (!empty($_GET))    remove_magic_quotes($_GET);
-    if (!empty($_POST))   remove_magic_quotes($_POST);
-    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
-    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
-    if (!empty($_SESSION)) remove_magic_quotes($_SESSION);
-    ini_set('magic_quotes_gpc', 0);
-  }
-  set_magic_quotes_runtime(0);
-  ini_set('magic_quotes_sybase',0);
-
-  //disable gzip if not available
-  if($conf['usegzip'] && !function_exists('gzopen')){
-    $conf['usegzip'] = 0;
-  }
-
-  //remember original umask
-  $conf['oldumask'] = umask();
-
-  //make absolute mediaweb
-  if(!preg_match('#^(https?://|/)#i',$conf['mediaweb'])){
-    $conf['mediaweb'] = getBaseURL().$conf['mediaweb'];
-  }
-
-/**
- * remove magic quotes recursivly
- *
- * @author Andreas Gohr <andi@splitbrain.org>
- */
-function remove_magic_quotes(&$array) {
-  foreach (array_keys($array) as $key) {
-    if (is_array($array[$key])) {
-      remove_magic_quotes($array[$key]);
-    }else {
-      $array[$key] = stripslashes($array[$key]);
-    }
-  } 
-} 
-
-/**
- * Returns the full absolute URL to the directory where
- * DokuWiki is installed in (includes a trailing slash)
- *
- * @author Andreas Gohr <andi@splitbrain.org>
- */
-function getBaseURL($abs=false){
-  global $conf;
-  //if canonical url enabled always return absolute
-  if($conf['canonical']) $abs = true;
-
-  $dir = dirname($_SERVER['PHP_SELF']).'/';
-
-  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
-  $dir = preg_replace('#//+#','/',$dir);
-
-  //finish here for relative URLs
-  if(!$abs) return $dir;
-
-  $port = ':'.$_SERVER['SERVER_PORT'];
-  //remove port from hostheader as sent by IE
-  $host = preg_replace('/:.*$/','',$_SERVER['HTTP_HOST']);
-
-  // see if HTTPS is enabled - apache leaves this empty when not available,
-  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
-  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
-    $proto = 'http://';
-    if ($_SERVER['SERVER_PORT'] == '80') {
-      $port='';
-    }
-  }else{
-    $proto = 'https://';
-    if ($_SERVER['SERVER_PORT'] == '443') {
-      $port='';
-    }
-  }
-
-  return $proto.$host.$port.$dir;
-}
+  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
+  require_once(DOKU_INC.'conf/dokuwiki.php');
+  require_once(DOKU_INC.'inc/io.php');
+  require_once(DOKU_INC.'inc/utf8.php');
+  require_once(DOKU_INC.'inc/mail.php');
 
 /**
  * Return info about the current document as associative
@@ -257,20 +168,23 @@ function idfilter($id,$ue=true){
 }
 
 /**
- * This builds a link to a wikipage (using getBaseURL)
+ * This builds a link to a wikipage
  *
  * @author Andreas Gohr <andi@splitbrain.org>
  */
-function wl($id='',$more='',$script='doku.php',$canonical=false){
+function wl($id='',$more='',$abs=false){
   global $conf;
   $more = str_replace(',','&amp;',$more);
 
   $id    = idfilter($id);
-  $xlink = getBaseURL($canonical);
+  if($abs){
+    $xlink = DOKU_URL;
+  }else{
+    $xlink = DOKU_BASE;
+  }
 
   if(!$conf['userewrite']){
-    $xlink .= $script;
-    $xlink .= '?id='.$id;
+    $xlink .= DOKU_SCRIPT.'?id='.$id;
     if($more) $xlink .= '&amp;'.$more;
   }else{
     $xlink .= $id;
@@ -283,12 +197,14 @@ function wl($id='',$more='',$script='doku.php',$canonical=false){
 /**
  * Just builds a link to a script
  *
+ * @todo   maybe obsolete
  * @author Andreas Gohr <andi@splitbrain.org>
  */
 function script($script='doku.php'){
-  $link = getBaseURL();
-  $link .= $script;
-  return $link;
+#  $link = getBaseURL();
+#  $link .= $script;
+#  return $link;
+  return DOKU_BASE.DOKU_SCRIPT;
 }
 
 /**
@@ -825,14 +741,14 @@ function notify($id,$rev="",$summary=""){
   $text = str_replace('@BROWSER@',$_SERVER['HTTP_USER_AGENT'],$text);
   $text = str_replace('@IPADDRESS@',$_SERVER['REMOTE_ADDR'],$text);
   $text = str_replace('@HOSTNAME@',gethostbyaddr($_SERVER['REMOTE_ADDR']),$text);
-  $text = str_replace('@NEWPAGE@',wl($id,'','doku.php',true),$text);
-  $text = str_replace('@DOKUWIKIURL@',getBaseURL(true),$text);
+  $text = str_replace('@NEWPAGE@',wl($id,'',true),$text);
+  $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text);
   $text = str_replace('@SUMMARY@',$summary,$text);
   $text = str_replace('@USER@',$_SERVER['REMOTE_USER'],$text);
   
   if($rev){
     $subject = $lang['mail_changed'].' '.$id;
-    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",'doku.php',true),$text);
+    $text = str_replace('@OLDPAGE@',wl($id,"rev=$rev",true),$text);
     require_once("inc/DifferenceEngine.php");
     $df  = new Diff(split("\n",rawWiki($id,$rev)),
                     split("\n",rawWiki($id)));
diff --git a/inc/format.php b/inc/format.php
index 44d39e22f90063e1cecc0a34645bca3bb374b5d9..4a5415ab4b2f3ac8074a5bf82689ca92739ab19a 100644
--- a/inc/format.php
+++ b/inc/format.php
@@ -1,4 +1,4 @@
-<?
+<?php
 /**
  * link format functions
  *
@@ -6,8 +6,9 @@
  * @author     Andreas Gohr <andi@splitbrain.org>
  */
 
-  require_once("conf/dokuwiki.php");
-  require_once("inc/common.php");
+  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
+  require_once(DOKU_INC.'conf/dokuwiki.php');
+  require_once(DOKU_INC.'inc/common.php');
 
 
 /**
@@ -248,7 +249,7 @@ function format_link_interwiki($link){
   $iwlinks = file('conf/interwiki.conf');
 
   //add special case 'this'
-  $iwlinks[] = 'this '.getBaseURL(true).'{NAME}'; 
+  $iwlinks[] = 'this '.DOKU_URL.'{NAME}'; 
   
   //go through iwlinks and find URL for wiki
   foreach ($iwlinks as $line){
@@ -266,13 +267,13 @@ function format_link_interwiki($link){
 
   //if ico exists set additonal style
   if(@file_exists('interwiki/'.$ico.'.png')){
-    $link['style']='background: transparent url('.getBaseURL().'interwiki/'.$ico.'.png) 0px 1px no-repeat;';
+    $link['style']='background: transparent url('.DOKU_BASE.'interwiki/'.$ico.'.png) 0px 1px no-repeat;';
   }elseif(@file_exists('interwiki/'.$ico.'.gif')){
-    $link['style']='background: transparent url('.getBaseURL().'interwiki/'.$ico.'.gif) 0px 1px no-repeat;';
+    $link['style']='background: transparent url('.DOKU_BASE.'interwiki/'.$ico.'.gif) 0px 1px no-repeat;';
   }
 
   //do we stay at the same server? Use local target
-  if( strpos($url,getBaseURL(true)) === 0 ){
+  if( strpos($url,DOKU_URL) === 0 ){
     $link['target'] = $conf['target']['wiki'];
   }
 
diff --git a/inc/html.php b/inc/html.php
index b21ab4e574d64011913fb88e78db569e41d8e816..d848f89b48a3a21e5fc719542c91c85a097d84f3 100644
--- a/inc/html.php
+++ b/inc/html.php
@@ -1,4 +1,4 @@
-<?
+<?php
 /**
  * HTML output functions
  *
@@ -6,7 +6,8 @@
  * @author     Andreas Gohr <andi@splitbrain.org>
  */
 
-  include_once("inc/format.php");
+  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
+  require_once(DOKU_INC.'inc/format.php');
 
 /**
  * Convenience function to quickly build a wikilink
@@ -188,13 +189,13 @@ function html_head(){
     <title><?=$ID?> [<?=$conf['title']?>]</title>
     <meta http-equiv="Content-Type" content="text/html; charset=<?=$lang['encoding']?>" />
     <meta name="generator" content="DokuWiki <?=getVersion()?>" />
-    <link rel="stylesheet" media="screen" type="text/css" href="<?=getBaseURL()?>style.css" />
-    <link rel="stylesheet" media="print" type="text/css" href="<?=getBaseURL()?>print.css" />
-    <link rel="shortcut icon" href="<?=getBaseURL()?>images/favicon.ico" />
+    <link rel="stylesheet" media="screen" type="text/css" href="<?=DOKU_BASE?>style.css" />
+    <link rel="stylesheet" media="print" type="text/css" href="<?=DOKU_BASE?>print.css" />
+    <link rel="shortcut icon" href="<?=DOKU_BASE?>images/favicon.ico" />
     <link rel="start" href="<?=wl()?>" />
     <link rel="contents" href="<?=wl($ID,'do=index')?>" title="<?=$lang['index']?>" />
-    <link rel="alternate" type="application/rss+xml" title="Recent Changes" href="<?=getBaseURL()?>feed.php" />
-    <link rel="alternate" type="application/rss+xml" title="Current Namespace" href="<?=getBaseURL()?>feed.php?mode=list&amp;ns=<?=$INFO['namespace']?>" />
+    <link rel="alternate" type="application/rss+xml" title="Recent Changes" href="<?=DOKU_BASE?>feed.php" />
+    <link rel="alternate" type="application/rss+xml" title="Current Namespace" href="<?=DOKU_BASE?>feed.php?mode=list&amp;ns=<?=$INFO['namespace']?>" />
     <link rel="alternate" type="text/html" title="Plain HTML" href="<?=wl($ID,'do=export_html')?>" />
     <link rel="alternate" type="text/plain" title="Wiki Markup" href="<?=wl($ID, 'do=export_raw')?>" />
 <?
@@ -213,14 +214,14 @@ function html_head(){
     <script language="JavaScript" type="text/javascript">
       var alertText   = '<?=$lang['qb_alert']?>';
       var notSavedYet = '<?=$lang['notsavedyet']?>';
-      var baseURL     = '<?=getBaseURL()?>';
+      var DOKU_BASE     = '<?=DOKU_BASE?>';
     </script>
-    <script language="JavaScript" type="text/javascript" src="<?=getBaseURL()?>script.js"></script>
+    <script language="JavaScript" type="text/javascript" src="<?=DOKU_BASE?>script.js"></script>
 
     <!--[if gte IE 5]>
     <style type="text/css">
       /* that IE 5+ conditional comment makes this only visible in IE 5+ */
-      img { behavior: url("<?=getBaseURL()?>pngbehavior.htc"); } /* IE bugfix for transparent PNGs */
+      img { behavior: url("<?=DOKU_BASE?>pngbehavior.htc"); } /* IE bugfix for transparent PNGs */
     </style>
     <![endif]-->
 
@@ -246,13 +247,11 @@ function html_btn($name,$id,$akey,$params,$method='get'){
   $id = idfilter($id,false);
 
   //make nice URLs even for buttons  
-  $link = getBaseURL().'/';
-  $link = preg_replace('#//$#','/',$link);
   if(!$conf['userewrite']){
-    $script = $link.'doku.php';
+    $script = DOKU_BASE.DOKUSCRIPT;
     $params['id'] = $id;
   }else{
-    $script = $link.$id;
+    $script = DOKU_BASE.$id;
   }
   
   $ret .= '<form class="button" method="'.$method.'" action="'.$script.'" onsubmit="return svchk()">';
@@ -529,7 +528,7 @@ function html_hilight($html,$query){
  * @author Andreas Gohr <andi@splitbrain.org>
  */
 function html_search(){
-  require_once("inc/search.php");
+  require_once(DOKU_INC.'inc/search.php');
   global $conf;
   global $QUERY;
   global $ID;
@@ -648,7 +647,7 @@ function html_revisions(){
     print ')</span> ';
 
     print '<a href="'.wl($ID,"rev=$rev,do=diff").'">';
-    print '<img src="'.getBaseURL().'images/diff.png" border="0" width="15" height="11" title="'.$lang['diff'].'" />';
+    print '<img src="'.DOKU_BASE.'images/diff.png" border="0" width="15" height="11" title="'.$lang['diff'].'" />';
     print '</a>';
     print '</li>';
   }
@@ -686,7 +685,7 @@ function html_recent(){
  * @author Andreas Gohr <andi@splitbrain.org>
  */
 function html_index($ns){
-  require_once("inc/search.php");
+  require_once(DOKU_INC.'inc/search.php');
   global $conf;
   global $ID;
   $dir = $conf['datadir'];
@@ -814,7 +813,7 @@ function html_buildlist($data,$class,$func,$lifunc='html_li_default'){
  * @author Andreas Gohr <andi@splitbrain.org>
  */
 function html_backlinks(){
-  require_once("inc/search.php");
+  require_once(DOKU_INC.'inc/search.php');
   global $ID;
   global $conf;
 
@@ -847,7 +846,7 @@ function html_backlinks(){
  * @author Andreas Gohr <andi@splitbrain.org>
  */
 function html_diff($text='',$intro=true){
-  require_once("inc/DifferenceEngine.php");
+  require_once(DOKU_INC.'inc/DifferenceEngine.php');
   global $ID;
   global $REV;
   global $lang;
@@ -1136,11 +1135,15 @@ function html_debug(){
   print_r($cnf);
   print '</pre>';
 
-  print '<b>abs baseURL:</b><pre>';
-  print getBaseURL(true);
+  print '<b>DOKU_BASE:</b><pre>';
+  print DOKU_BASE;
+  print '</pre>';
+  
+  print '<b>abs DOKU_BASE:</b><pre>';
+  print DOKU_URL;
   print '</pre>';
   
-  print '<b>rel baseURL:</b><pre>';
+  print '<b>rel DOKU_BASE:</b><pre>';
   print dirname($_SERVER['PHP_SELF']).'/';
   print '</pre>';
 
diff --git a/inc/init.php b/inc/init.php
new file mode 100644
index 0000000000000000000000000000000000000000..9314008e14e32400b09b2447a40d425cf53349c4
--- /dev/null
+++ b/inc/init.php
@@ -0,0 +1,110 @@
+<?php
+/**
+ * Initialize some defaults needed for DokuWiki
+ */
+
+  // define the include path
+  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
+  require_once(DOKU_INC.'conf/dokuwiki.php');
+
+  // define baseURL
+  if(!defined('DOKU_BASE')) define('DOKU_BASE',getBaseURL());
+  if(!defined('DOKU_URL'))  define('DOKU_URL',getBaseURL(true));
+
+  // define main script
+  if(!defined('DOKU_SCRIPT')) define('DOKU_SCRIPT','doku.php');
+
+  // set up error reporting to sane values
+  error_reporting(E_ALL ^ E_NOTICE);
+
+  // make session rewrites XHTML compliant
+  ini_set('arg_separator.output', '&amp;');
+
+  // init session
+  session_name("DokuWiki");
+  session_start();
+
+  // kill magic quotes
+  if (get_magic_quotes_gpc()) {
+    if (!empty($_GET))    remove_magic_quotes($_GET);
+    if (!empty($_POST))   remove_magic_quotes($_POST);
+    if (!empty($_COOKIE)) remove_magic_quotes($_COOKIE);
+    if (!empty($_REQUEST)) remove_magic_quotes($_REQUEST);
+    if (!empty($_SESSION)) remove_magic_quotes($_SESSION);
+    ini_set('magic_quotes_gpc', 0);
+  }
+  set_magic_quotes_runtime(0);
+  ini_set('magic_quotes_sybase',0);
+
+  // disable gzip if not available
+  if($conf['usegzip'] && !function_exists('gzopen')){
+    $conf['usegzip'] = 0;
+  }
+
+  // remember original umask
+  $conf['oldumask'] = umask();
+
+  // make absolute mediaweb
+  if(!preg_match('#^(https?://|/)#i',$conf['mediaweb'])){
+    $conf['mediaweb'] = getBaseURL().$conf['mediaweb'];
+  }
+
+
+
+/**
+ * remove magic quotes recursivly
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function remove_magic_quotes(&$array) {
+  foreach (array_keys($array) as $key) {
+    if (is_array($array[$key])) {
+      remove_magic_quotes($array[$key]);
+    }else {
+      $array[$key] = stripslashes($array[$key]);
+    }
+  }
+}
+
+/**
+ * Returns the full absolute URL to the directory where
+ * DokuWiki is installed in (includes a trailing slash)
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function getBaseURL($abs=false){
+  global $conf;
+  //if canonical url enabled always return absolute
+  if($conf['canonical']) $abs = true;
+
+  $dir = dirname($_SERVER['PHP_SELF']).'/';
+
+  $dir = str_replace('\\','/',$dir); #bugfix for weird WIN behaviour
+  $dir = preg_replace('#//+#','/',$dir);
+
+  //finish here for relative URLs
+  if(!$abs) return $dir;
+
+  $port = ':'.$_SERVER['SERVER_PORT'];
+  //remove port from hostheader as sent by IE
+  $host = preg_replace('/:.*$/','',$_SERVER['HTTP_HOST']);
+
+  // see if HTTPS is enabled - apache leaves this empty when not available,
+  // IIS sets it to 'off', 'false' and 'disabled' are just guessing
+  if (preg_match('/^(|off|false|disabled)$/i',$_SERVER['HTTPS'])){
+    $proto = 'http://';
+    if ($_SERVER['SERVER_PORT'] == '80') {
+      $port='';
+    }
+  }else{
+    $proto = 'https://';
+    if ($_SERVER['SERVER_PORT'] == '443') {
+      $port='';
+    }
+  }
+
+  return $proto.$host.$port.$dir;
+}
+
+
+?>
diff --git a/inc/io.php b/inc/io.php
index dfdbc4e1c67d8c0667d950686c6b5459d72b38f4..6586a554e08fb6394372fef6c07d74594032d810 100644
--- a/inc/io.php
+++ b/inc/io.php
@@ -1,4 +1,4 @@
-<?
+<?php
 /**
  * File IO functions
  *
@@ -6,8 +6,9 @@
  * @author     Andreas Gohr <andi@splitbrain.org>
  */
 
-  require_once("inc/common.php");
-  require_once("inc/parser.php");
+  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
+  require_once(DOKU_INC.'inc/common.php');
+  require_once(DOKU_INC.'inc/parser.php');
 
 /**
  * Returns the parsed text from the given sourcefile. Uses cache
diff --git a/inc/mail.php b/inc/mail.php
index b66678b02d4b8a0212107915c0b56ac163bf3acc..451fa6cb62a234d8bd8302e5066605c28e59f6b8 100644
--- a/inc/mail.php
+++ b/inc/mail.php
@@ -1,4 +1,4 @@
-<?
+<?php
 /**
  * Mail functions
  *
@@ -6,9 +6,10 @@
  * @author     Andreas Gohr <andi@splitbrain.org>
  */
 
-require_once('inc/utf8.php');
+  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
+  require_once(DOKU_INC.'inc/utf8.php');
 
-define('MAILHEADER_EOL',"\n"); //end of line for mail headers
+  define('MAILHEADER_EOL',"\n"); //end of line for mail headers
 
 /**
  * UTF-8 autoencoding replacement for PHPs mail function
diff --git a/inc/parser.php b/inc/parser.php
index 73bbee7657fff03ae6abb5654dcaf8079a5209dc..5c3c7885130417908e5a4adecc2f139ab88286f2 100644
--- a/inc/parser.php
+++ b/inc/parser.php
@@ -1,4 +1,4 @@
-<?
+<?php
 /**
  * The DokuWiki parser
  *
@@ -6,11 +6,12 @@
  * @author     Andreas Gohr <andi@splitbrain.org>
  */
 
-  include_once("inc/common.php");
-  include_once("inc/html.php");
-  include_once("inc/format.php");
-  require_once("lang/en/lang.php");
-  require_once("lang/".$conf['lang']."/lang.php");
+  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
+  include_once(DOKU_INC.'inc/common.php');
+  include_once(DOKU_INC.'inc/html.php');
+  include_once(DOKU_INC.'inc/format.php');
+  require_once(DOKU_INC.'lang/en/lang.php');
+  require_once(DOKU_INC.'lang/'.$conf['lang'].'/lang.php');
 
 /**
  * The main parser function.
@@ -469,7 +470,7 @@ function smileys(&$table,&$text){
     $smiley = trim($smiley);
     if(empty($smiley)) continue;
     $sm     = preg_split('/\s+/',$smiley,2);
-    $sm[1]  = '<img src="'.getBaseURL().'smileys/'.$sm[1].'" align="middle" alt="'.$sm[0].'" />';
+    $sm[1]  = '<img src="'.DOKU_BASE.'smileys/'.$sm[1].'" align="middle" alt="'.$sm[0].'" />';
     $sm[0]  = preg_quote($sm[0],'/');
     firstpass($table,$text,'/(\W)'.$sm[0].'(\W)/s',$sm[1],"\\1","\\2");
   }
diff --git a/inc/search.php b/inc/search.php
index 725cd22c95ef2efb8d5f33b970ed5dda24164311..d66d51af3dfdef66df03c4d389aa83560ce92e0f 100644
--- a/inc/search.php
+++ b/inc/search.php
@@ -1,4 +1,4 @@
-<?
+<?php
 /**
  * DokuWiki search functions
  *
@@ -6,7 +6,8 @@
  * @author     Andreas Gohr <andi@splitbrain.org>
  */
 
-  require_once("inc/common.php");
+  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
+  require_once(DOKU_INC.'inc/common.php');
 
 /**
  * recurse direcory
diff --git a/inc/utf8.php b/inc/utf8.php
index bb9b15799ce712dc2f5691250abdb623cc645116..222f40261959b1164cfb81289bbaa51bfc19cc29 100644
--- a/inc/utf8.php
+++ b/inc/utf8.php
@@ -1,4 +1,4 @@
-<?
+<?php
 /**
  * UTF8 helper functions
  *
diff --git a/media.php b/media.php
index 7b57181a9bd68ee62ed2c91a6168813872f1bf57..f5527fb8e1246d97a7ec80e91b6930a26e560cf9 100644
--- a/media.php
+++ b/media.php
@@ -1,36 +1,36 @@
 <?php
-ini_set('short_open_tag',"1");
-require_once("conf/dokuwiki.php");
-require_once("inc/common.php");
-require_once("lang/en/lang.php");
-require_once("lang/".$conf['lang']."/lang.php");
-require_once("inc/html.php");
-require_once("inc/search.php");
-require_once("inc/format.php");
-require_once("inc/auth.php");
-
-header('Content-Type: text/html; charset='.$lang['encoding']);
-
-$NS = $_REQUEST['ns'];
-$NS = cleanID($NS);
-
-if(auth_quickaclcheck("$NS:*") >= AUTH_UPLOAD){
-  $uploadok = true;
-  //create the given namespace (just for beautification)
-  $mdir = $conf['mediadir'].'/'.utf8_encodeFN(str_replace(':','/',$NS));
-  umask($conf['dmask']);
-  io_mkdir_p($mdir);
-  umask($conf['umask']);
-}else{
-  $uploadok = false;
-}
+  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__)).'/');
+  require_once(DOKU_INC.'conf/init.php');
+  require_once(DOKU_INC.'inc/common.php');
+  require_once(DOKU_INC.'lang/en/lang.php');
+  require_once(DOKU_INC.'lang/'.$conf['lang'].'/lang.php');
+  require_once(DOKU_INC.'inc/html.php');
+  require_once(DOKU_INC.'inc/search.php');
+  require_once(DOKU_INC.'inc/format.php');
+  require_once(DOKU_INC.'inc/auth.php');
+
+  header('Content-Type: text/html; charset='.$lang['encoding']);
+
+  $NS = $_REQUEST['ns'];
+  $NS = cleanID($NS);
+
+  if(auth_quickaclcheck("$NS:*") >= AUTH_UPLOAD){
+    $uploadok = true;
+    //create the given namespace (just for beautification)
+    $mdir = $conf['mediadir'].'/'.utf8_encodeFN(str_replace(':','/',$NS));
+    umask($conf['dmask']);
+    io_mkdir_p($mdir);
+    umask($conf['umask']);
+  }else{
+    $uploadok = false;
+  }
 
-if($_FILES['upload']['tmp_name'] && $uploadok){
-  media_upload($NS);
-}
+  if($_FILES['upload']['tmp_name'] && $uploadok){
+    media_upload($NS);
+  }
 
-//start output
-html_head();
+  //start output
+  html_head();
 ?>
 <body>
   <?html_msgarea()?>
@@ -53,8 +53,8 @@ html_head();
 </body>
 </html>
 <?
-//restore old umask
-umask($conf['oldumask']);
+  //restore old umask
+  umask($conf['oldumask']);
 
 /**********************************************/
 
@@ -153,14 +153,14 @@ function media_html_namespaces(){
 
   $data = array();
   #add default namespace
-  print '<b><a href="'.getBaseURL().'media.php?ns=">'.$lang['namespaces'].'</a></b>';
+  print '<b><a href="'.DOKU_BASE.'media.php?ns=">'.$lang['namespaces'].'</a></b>';
   search($data,$conf['mediadir'],'search_namespaces',array());
   print html_buildlist($data,'idx',media_html_list_namespaces);
 }
 
 function media_html_list_namespaces($item){
   $ret  = '';
-  $ret .= '<a href="'.getBaseURL().'media.php?ns='.idfilter($item['id']).'" class="idx_dir">';
+  $ret .= '<a href="'.DOKU_BASE.'media.php?ns='.idfilter($item['id']).'" class="idx_dir">';
   $ret .= $item['id'];
   $ret .= '</a>';
   return $ret;