Forum

UrlToolkit rewrite problems

Roy
26 April 2018, 11:31
Hello, I have set up a Virtualhost with a wildcard like this:
VirtualHost {
Hostname = *.example.com
WebsiteRoot = C:\wwwroot\example
StartFile = wildcard.php
AccessLogfile = C:\Program Files\Hiawatha10\log\access.log
ErrorLogfile = C:\Program Files\Hiawatha10\log\error.log
ExecuteCGI = yes
UseToolkit = wildcard_toolkit
SetEnv PHPRC = C:\phpinifiles\exampledomain.ini
TLScertFile = /config/certs/exampledomain.pem
RequireTLS = no
}

So this by itself worked fine and it would open wildcard.php on any subdomain.
For this I then tried to set up an UrlToolkit:
UrlToolkit {
ToolkitID = wildcard_toolkit
RequestURI exists Return
Header Host ^dumb.example.com$ Rewrite /dumb.php
}

So when I try to start the server, I get error on that last line with Header Host, and I can't seem to find much documentation or examples on your website about how Header Host can be used. If I comment out this line, then any subdomain I try to open, will be showing wildcard.php as it should. So in this example I wanted dumb.example.com to rewrite to /dumb.php but I tried the same with Redirect

I've tried to do ^dumb\.example\.com$
as I saw you do it with slashes in https://www.hiawatha-webserver.org/forum/topic/1730
Roy
26 April 2018, 12:08
I should probably say that, I guess Rewrite or Redirect isnt allowed when using "Header host", not implemented.

What I ultimately wanted was like in this thread:
https://stackoverflow.com/questions/10642426/htaccess-rewrite-subdomain-to-directory

I'd like to make any subdomain request like subdir.domain.com rewrite to /subdir/
i.e. showing the url with subdomain and not a redirect. Do you have any suggestions on how to do this?
Hugo Leisink
26 April 2018, 22:40
The first hostname should not contain a wildcard. The first hostname is, for example, used in redirects (HTTP 301 error), etc. Use a valid hostname, followed by the wildcard hostname. Like this:
VirtualHost {
Hostname = www.example.org, *.example.org
...


Try this UrlToolkit:
UrlToolkit {
ToolkitID = wildcard_toolkit
RequestURI exists Return
Header Host !^dumb.example.com$ Return
Match .* Rewrite /dump.php
}
Roy
26 April 2018, 23:29
Thank you for replying However, I am still wondering how to achieve the other thing,
to make a request for any subdomain rewrite to a subdir (if exists) on the website root?

lets say you have the subdir /whatever/ with an index.php file in it and try to open
whatever.example.com/index.php would rewrite to /whatever/index.php

On the DNS side all you need to do is set an A record with a wildcard, like *.example.com
So is there a way to do this in the urltoolkit of hiawatha just like in apache
(like in the discussion I posted a link for)?
Roy
5 May 2018, 13:50
No, I'm sorry but after all my attempts to make this work with a routing php file, I see that it does mess up everything related to paths

In hiawatha.conf
UrlToolkit {
ToolkitID = wildcard
Match /(.*) Rewrite /route.php?$1
RequestURI exists Return
}

VirtualHost {
Hostname = *.example.com #replaced real hostname
WebsiteRoot = C:\wwwroot\www
StartFile = index.php
AccessLogfile = C:\Program Files\Hiawatha10\log\access.log #none
ErrorLogfile = C:\Program Files\Hiawatha10\log\error.log
ExecuteCGI = yes
UseToolkit = wildcard
SetEnv PHPRC = C:\phpinifiles\php.ini
TLScertFile = /config/certs/hiawatha.pem
RequireTLS = no
}


In route.php
<?php
$request_uri = explode("?", $_SERVER['REQUEST_URI'], 2);
$requested_subdomain = explode(".", $_SERVER['HTTP_HOST']);
$pathToFile = $requested_subdomain[0] . "/" . $request_uri[0];
$defaultPage = "index.php";

switch ($request_uri[0]) {
case '/': // Default page
require $requested_subdomain[0] . '/'. $defaultPage;
break;
case '/dumb': //Just an example, custom rewrites can be added this way
require 'dumb.php';
break;
default: // All regular requests
$pathToFile = str_replace("/route.php", "/", $pathToFile ); //don't allow people to ask for THIS file "route.php"
if( substr($pathToFile, -1) == "/" ) $pathToFile = $pathToFile . $defaultPage; //if request ends with "/", we add $defaultpage

if( !file_exists ( $pathToFile ) ){ //show that nasty custom 404 page if file doesnt exist
header('HTTP/1.0 404 Not Found');
require 'my404.html';
break;
} else {
$the_content_type = get_mime_type($pathToFile); //determine what mime type to tell browser we're sending it
header('Content-Type: ' . $the_content_type); //ensure that file types like pics show as they should
require $pathToFile;
break;
}
}

function get_mime_type($filename) { //quick and dirty, function that determines from a list of file extensions
$idx = explode( '.', $filename );
$count_explode = count($idx);
$idx = strtolower($idx[$count_explode-1]);

$mimet = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html',
'php' => 'text/html',
'css' => 'text/css',
'js' => 'application/javascript',
'json' => 'application/json',
'xml' => 'application/xml',
'swf' => 'application/x-shockwave-flash',
'flv' => 'video/x-flv',

// images
'png' => 'image/png',
'jpe' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpg' => 'image/jpeg',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'ico' => 'image/vnd.microsoft.icon',
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'svg' => 'image/svg+xml',
'svgz' => 'image/svg+xml',

// archives
'zip' => 'application/zip',
'rar' => 'application/x-rar-compressed',
'exe' => 'application/x-msdownload',
'msi' => 'application/x-msdownload',
'cab' => 'application/vnd.ms-cab-compressed',

// audio/video
'mp3' => 'audio/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',

// adobe
'pdf' => 'application/pdf',
'psd' => 'image/vnd.adobe.photoshop',
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',

// ms office
'doc' => 'application/msword',
'rtf' => 'application/rtf',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'docx' => 'application/msword',
'xlsx' => 'application/vnd.ms-excel',
'pptx' => 'application/vnd.ms-powerpoint',


// open office
'odt' => 'application/vnd.oasis.opendocument.text',
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
);

if (isset( $mimet[$idx] )) {
return $mimet[$idx];
} else {
return 'application/octet-stream';
}
}

?>

This is as far as I got, and maybe somebody sees a bug in there or has improvements or would like to use the code for something, then go ahead, I'm really sad and disappointed that hiawatha can't do this particular thing as good as I hoped.

The setup that I made will work with less complicated webpages, like if you build up something on a subdomain from scratch and can adjust all paths etc in the php yourself, but I tried for instance to install a php webmail software and a forum software, in both cases it failed. From all my bughunting the only thing I can see is that it will mess up the paths etc.

So in my opinion it is not a solution to offer others, when they can't install basic softwares like forums etc.
Hugo Leisink
6 May 2018, 23:57
I think you need to rethink your hosting strategy. My advice is to keep it simple. Simply add a virtual host entry to the configuration and restart the webserver. What you are trying to do now is asking for trouble.
This topic has been closed.