From 344f64853a083222c0205905dab4ecd41d6570d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Tue, 13 Jul 2021 18:01:42 +0200 Subject: httpd.conf: Add virtual host configuration --- usr/pkg/etc/httpd/httpd.conf | 123 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 2 deletions(-) (limited to 'usr/pkg/etc') diff --git a/usr/pkg/etc/httpd/httpd.conf b/usr/pkg/etc/httpd/httpd.conf index 0d85006..f5166f0 100644 --- a/usr/pkg/etc/httpd/httpd.conf +++ b/usr/pkg/etc/httpd/httpd.conf @@ -108,7 +108,7 @@ LoadModule dir_module lib/httpd/mod_dir.so #LoadModule speling_module lib/httpd/mod_speling.so #LoadModule userdir_module lib/httpd/mod_userdir.so LoadModule alias_module lib/httpd/mod_alias.so -#LoadModule rewrite_module lib/httpd/mod_rewrite.so +LoadModule rewrite_module lib/httpd/mod_rewrite.so LoadModule perl_module lib/httpd/mod_perl.so @@ -136,7 +136,7 @@ DocumentRoot "/var/www/rbsd/htdocs" - DirectoryIndex index.html + DirectoryIndex index.php index.html @@ -235,3 +235,122 @@ SSLRandomSeed startup builtin SSLRandomSeed connect builtin + +use warnings; +use subs qw/vhost idn with_idn block/; +use Tie::DxHash; + +# Long-term redirections + +my %redir = ( + 'git.ankarstrom.se' => block( + RewriteRule => '^Apache-Inject\.git/?$ /cpan/Apache-Inject/ [L,R,END]', + RewriteRule => '^([^/]*)\.git(/.*)?$ /$1$2 [L,R,END]', + ), + 'john.ankarstrom.se' => block( + Redirect => '301 /feed.rss /articles.xml', + Redirect => '301 /feed.php /articles.xml', + Redirect => '301 /english/texts/replacing-javascript /replacing-javascript', + Redirect => '301 /web/separation-of-concerns.html /separation-of-concerns', + Redirect => '301 /unix/learning-c.html /learning-c', + Redirect => '301 /scripts http://git.ankarstrom.se/', + Redirect => '301 /software http://git.ankarstrom.se', + Redirect => '301 /ansi-sv-layout /sv-ansi', + ), + 'software.ankarstrom.se' => block( + RewriteRule => '^(win32/)?ahk(/.*)? http://git.ankarstrom.se/ahk/about/ [L,R]', + RewriteRule => '^(win32/)?drm(/.*)? http://git.ankarstrom.se/drm/about/ [L,R]', + RewriteRule => '^(win32/)?run(/.*)? http://git.ankarstrom.se/run/about/ [L,R]', + RewriteRule => '^(win32/)?tt(/.*)? http://git.ankarstrom.se/tt/about/ [L,R]', + RewriteRule => '^(win32/)?watch(/.*)? http://git.ankarstrom.se/watch/about/ [L,R]', + RewriteRule => '^.* http://git.ankarstrom.se/ [L,R]', + ), +); + +# Virtual host configuration + +vhost block(ServerName => 'ankarstrom.se', + ServerAlias => [with_idn 'www.ankarstrom.se'], + DocumentRoot => '/var/www/rbsd/htdocs', +); + +vhost block(ServerName => 'lamnafacebook.nu', + DocumentRoot => '/var/www/facebook/htdocs', + ServerAlias => [with_idn 'www.lamnafacebook.nu'], +); + +vhost block(ServerName => 'git.ankarstrom.se', + Directory => block('/var/www/git/htdocs' => block( + DirectoryIndex => 'cgit.cgi', + Options => '+ExecCGI', + RewriteEngine => 'On', + RewriteCond => '%{REQUEST_FILENAME} !-f', + RewriteCond => '%{REQUEST_FILENAME} !-d', + RewriteRule => '(.*) cgit.cgi/$1 [END,QSA]', + )), +); + +vhost block(ServerName => 'dev.ankarstrom.se'); +vhost block(ServerName => 'img.ankarstrom.se'); +vhost block(ServerName => 'john.ankarstrom.se'); +vhost block(ServerName => 'mail.ankarstrom.se'); +vhost block(ServerName => 'software.ankarstrom.se'); + +# Default virtual host configuration + +sub vhost { + my $vhost = $_[0]; # this should be a hash ref tied to Tie::DxHash + + if (!$vhost->{ServerName}) { + warn 'Call to vhost missing ServerName, skipping virtual host'; + return; + } + + # Merge directives of which only one instance is allowed + if (!$vhost->{DocumentRoot}) { + (my $subdomain = $vhost->{ServerName}) =~ s/\..*//; + $vhost->{DocumentRoot} = "/var/www/$subdomain/htdocs"; + } + + # Merge directives of which multiple instances are allowed + my $default = block( + ServerAlias => idn($vhost->{ServerName}), + Directory => block( + $vhost->{DocumentRoot} => block( + Options => 'Indexes FollowSymLinks', + AllowOverride => 'All', + Require => 'all granted', + exists $redir{$vhost->{ServerName}} ? (RewriteEngine => 'On') : (), + %{$redir{$vhost->{ServerName}}}, + ), + ), + ); + + my @keys = keys %$vhost; + my @values = values %$vhost; + for (my $i = 0; $i < @keys; $i++) { + $default->{$keys[$i]} = $values[$i]; + } + $vhost = $default; + + $VirtualHost{'*:80'} = [@{$VirtualHost{'*:80'}||[]}, $vhost]; +} + +sub idn { + my ($domain) = @_; + $domain =~ s/ankarstrom\.se$/xn--ankarstrm-77a.se/; + $domain =~ s/lamnafacebook\.nu$/xn--lmnafacebook-gcb.nu/; + return $domain; +} + +sub with_idn { + return $_[0], idn $_[0]; +} + +sub block { + my %hash; + tie %hash, 'Tie::DxHash'; + %hash = @_; + return \%hash; +} + -- cgit v1.2.3