Dynamically configured virtual hosting - apache

Helo!
I would like to prepare a dynamic virtual host for all subdomains that will be created in future using mod_rewrite. All subdomains would be configured prety much same way, so i thought of using dynamic VH configuration.
I want my document root for each subdomain to be /home/user/public_html/subdomainName.
I've tried with following configuration, but had no success:
<VirtualHost xxx.xxx.xxx.xxx:80>
# get the server name from the Host: header
UseCanonicalName Off
<Directory /home/user/public_html/>
# ExecCGI is needed here because we can't force
# CGI execution in the way that ScriptAlias does
Options FollowSymLinks ExecCGI
</Directory>
RewriteEngine On
# a ServerName derived from a Host: header may be any case at all
RewriteMap lowercase int:tolower
#rule that is suposed to set document root of virtual host!???
RewriteRule ^([a-z-]+)\.domain\.com/?(.*) /home/user/public_html/$1/$2
</VirtualHost>
The rule or somethinh seems to be wrong andit doesn't apply. I've never worked with dynamic VH before so i have no idea where i'm wrong...

This is a need a lot of people had before you. So there's an apache module which can do that for you mod_vhost_alias http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html
Provides for dynamically configured
mass virtual hosting

instead of dynamic virtual hosts, you can also do this using wildcard dns which means configuring {anything}.yourdomain.tld to point to your server.
This will also get you same document root for each domain.
note that you will need to add ServerAlias *.yourdomain.tld to the virtual host entry

With RewriteRule you can't access the domainname. Also use RewriteCond for that part and use %1 etc. for backreference.
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.domain\.com$ [NC]
RewriteRule (.*) %1/$1
The only problem is this will create a continues loop of prepending the subdomainname string. So what I normally do is create a separate folder for subdomains, like 'subdomains'
RewriteRule ^subdomains - [L]
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.domain\.com$ [NC]
RewriteRule (.*) subdomains/%1/$1

Related

Apache dynamic wildcard host rewrite with dynamic subdomains

I'm currently in the process of converting an old apache vhost, where one is currently created for each new release, to a dynamic host that can handle all the subdomains so we don't have to create a new one all the time. I need some help! We're using apache v2.2.
The Goal
Dynamic all the things. To have a single virtual host that handles all redirects for a specific set of subdomains. The url is below, note that sub1 and branch are dynamic and could be anything.
sub1.branch.sandbox.domain.com
The directory structure for this is as follows:
/var/www/vhosts/branch/sub1.branch.sandbox.domain.com
As you can see above, the directory structure has the branch as a sub-directory before the full url is the name of another sub-directory.
Also, /images/ in the url needs to forward to shared-httpdocs/images for each domain.
The vhost I have so far
<VirtualHost *:80>
ServerName branch.sandbox.domain.com
ServerAlias *.branch.sandbox.domain.com
VirtualDocumentRoot /var/www/vhosts/branch/%0
Options Indexes FollowSymLinks
# Rewrite Engine Stuff Here
RewriteEngine On
DirectoryIndex index.php
# Assets needs to be forwarded - currently not working
RewriteCond %{REQUEST_URI} ^/(css|js|images)/.*
RewriteRule .*$ /var/www/vhosts/%0/shared-httpdocs/%1$ [L]
# The HTTP dispatcher - currently not working
RewriteCond %{HTTP_HOST} ^(.*)\.branch\.sandbox\.domain\.com [NC]
RewriteRule ^(.*)$ /var/www/vhosts/%1/applications/portal/dispatchers/http.php [L,QSA,NS,NE,NC]
</VirtualHost>
The old host I'm trying to copy
This is the old vhost I'm trying to convert from. It's horrible, messy, and our ops has to create a new DNS entry every time. What a joke! I need to sort this out...
<VirtualHost *:80>
# Notice how the stupid convention below will require a new DNS entry each time?
ServerName sandbox.branch.domain.com
ServerAlias sandbox.api.branch.domain.com
DocumentRoot /var/www/vhosts/sandbox.branch.domain.com/applications/portal/httpdocs
<Directory "/var/www/vhosts/sandbox.branch.domain.com/applications/portal/httpdocs">
allow from all
order allow,deny
# Enables .htaccess files for this site
#AllowOverride All
RewriteEngine On
# Rewrite all non-static requests to go through the webapp
RewriteCond %{REQUEST_URI} ^/(css|js|images)/.*
RewriteRule .* - [L]
# Rewrite everything else to go through the webapp
RewriteRule ^(.*)$ /dispatchers/http.php [QSA,L]
</Directory>
<Directory "/var/www/vhosts/sandbox.branch.domain.com/applications/portal/dispatchers">
allow from all
</Directory>
# Allow us to rewrite to the webapp without it being in the webroot
Alias /dispatchers /var/www/vhosts/sandbox.branch.domain.com/applications/portal/dispatchers
# Get shared/ to point to the shared static resources
Alias /shared /var/www/vhosts/sandbox.branch.domain.com/shared-httpdocs
</VirtualHost>
A new DNS entry is required each time we have a new branch, so I'm trying to mitigate this by providing a dynamic subdomain vhost (see the vhost I have so far). I've gone from not even being able to match /images/ in the url to a permanent redirect loop.
How can I achieve my goal? I know it's a little complex. If I can't do it, I'll just have to write a script that will generate a new vhost each time but a dynamic one that 'just works' would be fantastic. I've put two days into this so far, I'm no sysadmin. Your help would be greatly appreciated.
Resources I have been using:
mod_rewrite official docs - Shows the basics like things on conditions with REWRITE_COND
Sub domain rewriting - A question on subdomain rewriting
Asset rewriting - Another question on rewriting things like images / css / js, which doesn't seem to work for me
It's not a complete answer, but is too long for comment.
The %0 (%0 to %9) in a rewrite rule are back references to captures in the last RewriteCond. It seems to me you wanted instead the host name. Also it seems you miss the "branch" part of the path. In the Asset's rewrite you also throw away the filename part.
# Assets needs to be forwarded - currently not working
RewriteCond %{REQUEST_URI} ^/(css|js|images)/(.*)
RewriteRule .*$ /var/www/vhosts/branch/%{HTTP_HOST}/shared-httpdocs/%1/%2$ [L]
# The HTTP dispatcher - currently not working
RewriteCond %{HTTP_HOST} ^(.*)\.branch\.sandbox\.domain\.com [NC]
RewriteRule ^(.*)$ /var/www/vhosts/branch/%{HTTP_HOST}/applications/portal/dispatchers/http.php [L,QSA,NS,NE,NC]
You can get debugging help also from mod_rewrite dedicated logging with RewriteLog and RewriteLogLevel directives.
Hope this will bring you further.

Why is Apache Permanent Redirect removing the slash between the domain and the path?

I'm using Apache 2.4, and I set up two virtual directories. One requires SSL, and the other one redirects to it.
If a user attempts to visit https://www.derp.com/derp without /derp existing, they correctly get a 404. But when a user visits http://www.derp.com/derp, Apache incorrectly redirects the user to https://www.derp.comderp, removing the slash between the path and the domain name.
I have no idea what would be causing this.
The following is the setup of my Virtual Host.
<VirtualHost *:443>
ServerAdmin derp#derp.com
ServerName www.derp.com
ServerAlias derp.com
DocumentRoot "C:\Users\derp\Documents\Web Projects\derp"
SSLEngine on
SSLCertificateFile "C:\Apache24\certs\cert.cer"
SSLCertificateKeyFile "C:\Apache24\certs\key.key"
</VirtualHost>
<VirtualHost *:80>
ServerAdmin derp#derp.com
ServerName www.derp.com
ServerAlias derp.com
Redirect permanent / https://www.derp.com/
</VirtualHost>
<Directory "C:\Users\derp\Documents\Web Projects\derp">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
SSLRequireSSL
</Directory>
Why would Apache be behaving this way?
Bonus Question: Should redirects be handled in my virtual host definition, or should it be handled in the .htaccess file in the web site's physical directory?
Edit:
I'm starting a Laravel project, and by default the public folder does contain a .htaccess file, so here's that guy:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Edit Two:
I tried:
adding a slash at the end of the DirectoryRoot path
replacing the backslashes with forward slashes in the DirectoryRoot path
replacing the backslashes with double backslashes in the DirectoryRoot path
I also removed the .htaccess file from the directory completely.
It redirects correctly when you go from http://www.derp.com to https://www.derp.com. It's just when you specify a path and attempt https that it removes the slash between the domain and the path.
Edit Three:
I also attempted the following suggestion:
Redirect permanent / https://www.derp.com/
Try
RedirectMatch permanent /(.*) https://www.derp.com/$1
or
RedirectMatch permanent (.*) https://www.derp.com/$1
... and instead of redirecting to https://www.derp.comderp, it instead does not redirect, attempts and gives a 404 for http://www.derp.com/derp, but using Apache's 404, instead of throwing a Not Found Exception, as Laravel does without configuration.
Edit Four:
I have also tried:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
In the .htaccess file and the behavior did not change at all.
I got it.
The issue did not lay with the rewriting at all, it was the SSLRequireSSL directive under my Directory definition that was causing the problem.
I simply removed this directive, refreshed the cache in all of my browsers, and the site then continued to work correctly. This was discovered through the process of elimination.
The documentation notes:
This directive forbids access unless HTTP over SSL (i.e. HTTPS) is enabled for the current connection. This is very handy inside the SSL-enabled virtual host or directories for defending against configuration errors that expose stuff that should be protected. When this directive is present all requests are denied which are not using SSL.
The emphasis is my own. SSLRequireSSL may have Apache only return a 403 or 404 if HTTP over SSL is not enabled, interfering with the Redirect rule. A rewrite rule such as the one in this answer on Server Fault may be a better alternative depending on your use case:
RewriteEngine On
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(/(.*))?$ https://%{HTTP_HOST}/$1 [R=301,L]
My issue was related to browser caching.
I tried it in a different browser and it worked and then tried again in a private session in the first browser and it also worked.

Rewriting specific host to directories on localhost

I'm currently trying to redirect my local configured domains to its directories.. What is the best way to achieve this using htaccess?
Currently 'localhost' redirects to /Library/WebServer/Documents/ (mac OSX), i want to create an htaccess file in this directory that will lead all domains to its directory.
Example htaccess code of what I'm looking for:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com/$
RewriteRule ^(.*)$ /Library/WebServer/Documents/example.com/$1 [L]
I hope someone can help me out, think there's an easy fix for this!
Kind regards,
This is not possible with .htaccess alone. You need access to the main server configuration file or at least modify the appropriate virtual host section.
If you can modify the main config file, you can add any number of virtual hosts.
If you're allowed to adjust your virtual host only, you must add as many ServerAlias entries to your virtual host as you have domains.
Then you can add the rewrite rules for the various domains
RewriteEngine On
# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com/$
RewriteRule ^.*$ /Library/WebServer/Documents/example.com/$0 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com/$
RewriteRule ^.*$ /Library/WebServer/Documents/domain.com/$0 [L]
Try this
# get the server name from the Host: header
UseCanonicalName Off
# include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /var/www/vhosts/%0/httpdocs
<Location /var/www/vhosts>
AllowOverride All
Options +FollowSymLinks
</Location>
Then, you could try these examples:
example.com would show /var/www/vhosts/example.com/httpdocs
example2.com would show /var/www/vhosts/example2.com/httpdocs
Remember to add the domains to your hosts file.
Source

apache - RewriteEngine doesn't work outside <VirtualHost> Directive

I'm trying to configure apache to redirect the requests containing only hostname (domain) to another path.
I found the following directives useful, but as long as those are enclosed in VirtualHost, put in site_available folder (/etc/apache2/) and enabled as a separate site (with a2ensite command).
<VirtualHost *:80>
ServerName myserver:80
WSGIScriptAlias / /var/path/to/myproject/myproject.wsgi
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ /path/path [R=301,L]
</VirtualHost>
Note that only Rewrite* directives don't work. The alias to my Python project works fine.
Morover, I found out that if I disable other virtual host(s) (000-default) with a2dissite command and put the directives mentioned above (without VirtualHost) to httpd.conf file everything (including the Rewrite* rules) work.
So, I am in doubt why I couldn't get this done in simple server config context, and what is the solution?
The reason why it appears that the Rewrite* directives do not work in the server configuration and need to be declared inside the VirtualHost is because (from the RewriteEngine documentation):
[...] rewrite configurations are not inherited by virtual hosts. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use rewrite rules.
To change this behaviour, you can set RewriteOptions to something InheritDownBefore or similar. You then still need to have a RewriteEngine On on each VirtualHost but at least you won't need to copy the rewrite rules on each one.
So, taking your code example, this should work:
RewriteEngine On
RewriteOptions InheritDownBefore
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ /path/path [R=301,L]
<VirtualHost *:80>
ServerName myserver:80
WSGIScriptAlias / /var/path/to/myproject/myproject.wsgi
RewriteEngine On
</VirtualHost>

How can I implement a global RewriteCond / RewriteRule in Apache that applies to all virtual hosts?

The title pretty much says it all. :-) I have lots of virtual hosts and I want to put a single rewriting block at the top of the httpd.conf file that rewrites URLs no matter which virtual host the request might be directed to. How the heck do I do this?
I found this but my question is the same: how can I do this without resorting to .htaccess files and performing some other action for each virtual host?
OMGTIA!
Specify RewriteOptions InheritDown in the parent scope (such as httpd.conf) to get your rules applied in child Virtual Hosts without modifing them.
This will only work on Virtual Hosts where the RewriteEngine directive is set to on:
Note that rewrite configurations are not inherited by virtual hosts. This means that you need to have a RewriteEngine on directive for each virtual host in which you wish to use rewrite rules.
(source)
Apache supports this since 2.4.8 (not available at the time of the original question).
From documentation for RewriteOptions:
InheritDown
If this option is enabled, all child configurations will inherit the configuration of the current configuration. It is equivalent to specifying RewriteOptions Inherit in all child configurations. See the Inherit option for more details on how the parent-child relationships are handled.
Available in Apache HTTP Server 2.4.8 and later.
InheritDownBefore
Like InheritDown above, but the rules from the current scope are applied before rules specified in any child's scope.
Available in Apache HTTP Server 2.4.8 and later.
IgnoreInherit
This option forces the current and child configurations to ignore all rules that would be inherited from a parent specifying InheritDown or InheritDownBefore.
Available in Apache HTTP Server 2.4.8 and later.
(http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions)
By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each <VirtualHost> section:
RewriteEngine On
RewriteOptions Inherit
click http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html to find more information
Looks like the simplest possible solution is to add
RewriteOptions inherit
to each VirtualHost directive. This is at least a lot simpler than messing with .htaccess files. Apache is pretty clear on the fact that
by default, rewrite configurations are
not inherited. This means that you
need to have a RewriteEngine on
directive for each virtual host in
which you wish to use it.
(http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html)
and apparently the way to change the default is via RewriteOptions in the child (vhost or director), so you have to do something in each child.
I've never tested it, so it might not work, but I would try adding an include directive in all of the virtual host blocks to a single file. You would have to change each virtual host configuration block once, but after that, you should have a central place from which to make changes. YMMV.
If you're only trying to rewrite something in the domain part of the name, e.g. to fix a common misspelling, you don't even need the 'inherit' option. I setup a no-name virtual host to catch all invalid host names and respell them correctly before redirecting them.
Since this uses redirects, the appropriate virtual host will be found after the rewrites have been applied.
Options +Indexes +FollowSymLinks
RewriteEngine on
# If it begins with only domain.com, prepend www and send to www.domain.com
RewriteCond %{HTTP_HOST} ^domain [NC]
RewriteRule ^(.*) http://www.domain.com$1 [L,R=301]
# Correct misspelling in the domain name, applies to any VirtualHost in the domain
# Requires a subdomain, i.e. (serviceXXX.)domain.com, or the prepended www. from above
RewriteCond %{HTTP_HOST} ^([^.]+\.)dommmmmain\.com\.?(:[0-9]*)?$ [NC]
RewriteRule ^(.*) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+\.)?domain.com(.*) http://$1domain.com$2 [L,R=301]
# No-name virtual host to catch all invalid hostnames and mod_rewrite and redirect them
<VirtualHost *>
RewriteEngine on
RewriteOptions inherit
</VirtualHost>
You may want to use InheritDownBefore to avoid having to add more junk to your vhosts.
An example of a global letsencrypt alias:
# letsencrypt
<IfModule alias_module>
Alias /.well-known/ /var/www/html/.well-known/
</IfModule>
<IfModule mod_rewrite.c>
# prevent vhost rewrites from killing the alias
RewriteEngine On
RewriteOptions InheritDownBefore
RewriteCond %{REQUEST_URI} ^/\.well\-known
RewriteRule . - [L,PT]
</IfModule>
Then you can do this in each of your vhosts, with no other directives:
<VirtualHost *:80>
....
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/.* /index.php [L,PT]
</IfModule>
</VirtualHost>
Thanks to everyone to answered above. It helped me find my answer.
Question has been answered already, I just wanted to add an example in case you are using Google Compute Engine. It says it requires Apache HTTP Server 2.4.8 BUT it works with Apache/2.4.25 (Debian). Even when I try to upgrade, I cannot go past Apache/2.4.25. It says this version is the latest version.
Here's an example of how to implement.
RewriteOptions InheritDown
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP_HOST} !\.co$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)\.[^.]+$ [NC]
RewriteRule ^ https://%1.co%{REQUEST_URI} [L,NE,R=301]
<VirtualHost *:80>
RewriteEngine On
ServerAlias *.*
</VirtualHost>
ALSO OF NOTE (For Testing):
When you are testing your rewrite engine. It is really easy to get confused about if it is working or not because of cache and cookies. If you got it to work once on a browser, it will continue to work even if you delete the rewrite code. Testing rewrite is really annoying sometimes. You might think it works but then it stops or starts.
Best way to test rewrite code is to open an incognito tab in your browser, clear or cookies and cache. Open developer mode just in case. DO NOT JUST REFRESH. You need to click into the URL and refresh. Or open new tab. Or copy/paste URL into new window. If you use same window with refresh, it might be just redoing results from the past instead of renewing the new code.
I've always used a "catch-all" VHost for directives I wanted across the board, like......
Listen 80
NameVirtualHost *:80
<VirtualHost *:80>
ErrorLog "/var/log/apache2/error_log"
</VirtualHost>
<VirtualHost *:80>
ServerName alloftherestoftheVHosts.com
DocumentRoot "/ServiceData/.........
............
And it's always seemed to work... error logs were getting combined properly, etc...... but it IS possible that this was the result of an earlier / conflicting / like-minded directive.
Personal note.. Whoever dreamed up the Apache configuration schema and syntax was a dingbat, or a group of dingbats, who spent too much time in their cave.... The whole thing should be exorcised and XMLized, or something! Although they are both wildly different... the Hello-Kitty setup process of Cherokee.. to the viciously succinct NGinx config.... are both so much more logical..