Forum

Rewrite subdomain to directory

Roy
28 April 2018, 23:16
I'm trying to do something like in this thread: https://stackoverflow.com/questions/10642426/htaccess-rewrite-subdomain-to-directory

How would you make these rewrite rules in hiawatha:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/subdomains/%1/$1 [L,NC,QSA]


What I want to achieve is for a request to xxx.domain.com to rewrite to /domian/xxx/
and with the subdomain showing in url, not a redirect that will show the url www.domain.com/xxx/

Is this possible with hiawatha?
Nicolas
30 April 2018, 19:36
Hello,

There is no functions in Hiawatha to emulate the %{...} parameters of Apache.

If Hugo didn't change his mind since this message last year, Hiawatha will never have such functions. In his opinion, it's not something a web server should take care but something an application should :

https://www.hiawatha-webserver.org/forum/topic/2561/#12663

I'm not sure (because I never tried it) but I think you can do something like that with a php script.

I did a loose search for "subdomains routes with php" and it's seems possibles. I found this blog post and it's author did create a php router capable to work with sub-domains :

http://radify.io/blog/routing-in-php-a-complete-benchmark/
https://github.com/crysalead/router

I don't know if it's the better router but on his github page, he posted a benchmark between multiples solutions for you to look at.
Joe Scmoe
1 May 2018, 13:57
It seems to be the topic du jour so I might suggest looking into using a reverse proxy.
Hugo Leisink
1 May 2018, 20:44
That’s not possible in a generic way, but you could do something like this:
Header Host !xxx.domain.com Skip 2
Match ^/subdomain/xxx/.* Skip 1
Match ^/(.*) Rewrite /subdomain/xxx/$1


You have to repeat this for every host within your domain.

Why do you want this kind of rewrite if I may ask?
Roy
2 May 2018, 06:53
Thanks for the replies, and Hugo, although your suggested solution with setting it per host like that would work, it is not a very good solution if you want to for instance offer hosting where users will have their own subdomain on the server, it should be automated so that it is just a matter of creating a folder to be the users homedir

I've been trying to follow Nicolai's suggestion and I now have a setup with a short php file that does the job, I'll post the script here if I eventually get satisfied with how it works. A reverse proxy like suggested by Joe Schmoe, is out of the question, this needs to be simpler than that.
Roy
5 May 2018, 13:56
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.
This topic has been closed.