Apache Settings For Unique subdomain and Domain Directories - apache

I'm fairly capable with most of the web server configuration but I have run into a problem.
I have a VPS install with one sub domain. Currently the link sub1.domain.com points to the root of the public_html/www folder.
I am now looking at creating new sub domains as well as using domain.com to point to a wordpress installation.
From my understanding, I must edit the apache httpd.conf file, but I am unsure exactly how to do this. I would expect that I would have to have domain.com point to a subdirectory as well as sub2.domain.com point to a different sub directory. Something like the following.
sub1.domain.com >> rootPathForApache/
domain.com >> rootPathForApache/domain.com/
sub2domain.com >> rootPathForApache/sub2/
Could somebody please point me in the right direction.
Kind Regards,

You have a couple options:
Option 1: Apache's <VirtualHost> directive could solve this (multiple). This is only available in the server config context.
<VirtualHost *:80>
DocumentRoot "rootPathForApache/"
ServerName sub1.domain.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "rootPathForApache/domain.com/"
ServerName domain.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "rootPathForApache/sub2/"
ServerName sub2domain.com
</VirtualHost>
Option 2: Use a .htaccess file in rootPathForApache with the mod_rewrite module loaded, and introduce ServerAlias directive into either your existing <VirtualHost> directive.
.htaccess contents (or if you want within the VirtualHost directive itself):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub1.domain.com
RewriteRule ^(.*) $1 [L]
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*) domain.com/$1 [L]
RewriteCond %{HTTP_HOST} ^sub2domain.com
RewriteRule ^(.*) sub2/$1 [L]
...and the ServerAlias:
ServerName domain.com
ServerAlias sub1.domain.com sub2domain.com
The latter solution (option 2) just seems ridiculous, especially since ServerAlias is only available within a VirtualHost directive and mod_rewritehtaccess files add unnecessary overhead. That and it doesn't seem natural. I guess it illustrates the flexibility of Apache's configuration and modules.

Related

How to correctly set Documentroot in Apache serving Plone

I have a Plone site called example.com located at /var/www/Plone (I think). I have the following settings for the site located in sites-available for vhosts (excerpt):
<VirtualHost 10.0.1.4:8082>
ServerAdmin webmaster#localhost
ServerName wiedhas.noip.me
DocumentRoot /var/www/Plone
When I try to reach my site wiedhas.noip.me, apache loads the Plone directory tree and not my Plone site. I can browse through the file system of /var/www/Plone but it is not loading the site. I must not have set the documentroot to the correct directory of my site? Any help much appreciated.
This an excellent docu about running plone behind apache and more.
http://docs.plone.org/manage/deploying/front-end/apache.html
A simple example with ssl, how a vhost could look like:
<VirtualHost $IP:80>
ServerName my.domain.com
Redirect / https://my.domain.com
</VirtualHost>
<VirtualHost $IP:443>
ServerName my.domain.com
ErrorLog logs/my.domain.com-http-error.log
CustomLog logs/my.domain.com-http-access.log combined
Include vhosts.d/....ssl.inc
RewriteEngine On
RewriteRule ^/(.*) http://127.0.0.1:$PORT_OF_PLONE/VirtualHostBase/https/%{SERVER_NAME}:%{SERVER_PORT}/zodb/path/top/plone/VirtualHostRoot/$1 [P,L]
</VirtualHost>
The most important part is the rewrite rule:
RewriteRule ^/(.*) http://127.0.0.1:$PORT_OF_PLONE/VirtualHostBase/https/%{SERVER_NAME}:%{SERVER_PORT}/zodb/path/top/plone/VirtualHostRoot/$1 [P,L]
$PORT_OF_PLONE = Port of your running plone instance
/zodb/path/top/plone = That's where you added the plone site in zope.
Took me a while to get mine going so maybe this helps:
My vhosts looks like this (where my plone site is called 'mywebsite'):
#---------------------------------
# www.mywebsite.com
#---------------------------------
<VirtualHost *:80>
ServerName www.mywebsite.com
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/(.*)$ http://127.0.0.1:8081/VirtualHostBase/http/%{SERVER_NAME}:80/mywebsite/VirtualHostRoot/$1 [L,P]
</IfModule>
</VirtualHost>
Hope that helps :)
As you see in the first and correct answer you do not need DocumentRoot. DocumentRoot points to a directory with files to render by Apache. But Plone brings it's own server, the Zope application server, which runs on a different port than Apache. The RewriteRule redirects the incoming request to the application server and modifies the response in a way that the redirection is hidden for the client.

Wild card Sub-domain using virtual hosts

Can someone explain to me how this actually works. This is the code in Apache's httpd.conf file.
<VirtualHost *:80>
DocumentRoot "somepath"
ServerAlias mobile.mysite.com
ServerName mobile.mysite.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "somepath"
ServerAlias *.mysite.com
ServerName mysite.com
</VirtualHost>
I want do something like if its mobile.mysite.com it should go to mobile.mysite.com. If its anything else.mysite.com, then it should go to mysite.com. Does the above code perform the same thing ? Any suggestions ?
No, this code does not do any of the mentioned task. You will have to use mod_rewrite for this purpose.
Modify your httpd.conf file to include mod_rewrite.so and then write something like:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule $ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
What the above rewrite rule does is that it checks whether the host name starts with www or not, if it does not then it includes www. in the hostname and redirects.
You could write a rewrite rule similar to this.
Read more about rewrite rule here

How to redirect a web page while not changing the URL (using Apache VirtualHost)

I'm setting up Virtual Hosts in my Apache web server and already have rules in place that just do a simple 301 redirect from one URL to another. However, I've now been asked if I can write a rule that redirects to another page while keeping the URL the same and I've tried this:
<VirtualHost *:80>
ServerName ZZZ.com
ServerAlias YYY.com ZZZ.com
Redirect / YYY.com
</VirtualHost>
And this:
<VirtualHost *:80>
ServerName ZZZ.com
RewriteEngine on
RewriteRule ^/(.*) YYY.com/$1 [R]
</VirtualHost>
Neither did what I expected of them. They look wrong to me but I'm just not finding any helpful information anywhere. I've looked at http://httpd.apache.org/docs/2.4/vhosts/examples.html and http://httpd.apache.org/docs/current/mod/mod_rewrite.html - Neither of them were very helpful.
<VirtualHost *:80>
ServerName YYY.com
ServerAlias ZZZ.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^yyy\.com$ [NC]
RewriteRule (.*) YYY.com$1 [R=302,L]
</VirtualHost>
You were missing a condition to prevent it from redirecting correct urls.

Variable/Dynamic/REGEX Virtualhost in Apache?

Just an off the wall question today. Is it posible to vary DocumentRoot of a virtualhost based on the subdomain requested like so?
<VirtualHost *>
ServerName ^VARIABLE$.example.com
DocumentRoot ~/Sites/^VARIABLE$
</VirtualHost>
Yes it is possible:
Step1: Setting up Wildcard DNS
You have to add an A Record that points to your server's IP like that:
*.example.com. IN A 192.168.1.1
Step2: Set up apache VirtualHost
<VirtualHost *>
ServerName www.example.com
ServerAlias *.example.com
DirectoryIndex index.html
DocumentRoot /home/www/www.example.com/htdocs
....
</VirtualHost>
Notice the important line: ServerAlias *.example.com. This will tell Apache that any host with the .example.com suffix will match this virtual host too.
Step3: Setting up Rewrite Rules
You have to add this lines in your .htaccess file located in your web root folder (eg. /home/www/www.example.com/htdocs):
RewriteEngine on
RewriteCond %{http_host} .
RewriteCond %{http_host} !^www.example.com [NC]
RewriteCond %{http_host} ^([^.]+)\.example.com [NC]
RewriteRule ^(.*) http://www.example.com/%1/ [R=301,L,QSA]
That way a request for foo.example.com will redirect visitors to example.com/foo and so on. Good luck.
(Reference: http://www.debian-administration.org/articles/358)

Apache Rewrite: Always use HTTPS (how to add an exception)

On mydomain.com, I currently keep all of my apache conf files in:
/etc/httpd/conf.d/
In there I have a file called alwaysHttps.conf that contains:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
I have a bunch of virtualhosts, and for one domain on the site: myotherdomain.com I would like to turn off the auto redirect. Is it possible to setup an exception to the redirect to https, rather than having to get rid of the global alwaysHttps.conf?
<VirtualHost *:80>
DocumentRoot /home/webadmin/myotherdomain.com/html
ServerName myotherdomain.com
CustomLog "/home/webadmin/myotherdomain.com/access_log" "combined"
ErrorLog "/home/webadmin/myotherdomain.com/error_log"
<Directory /home/webadmin/myotherdomain.com/html>
Options Includes FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !myotherdomain\.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [QSA]
Fantastic!
In my http.conf I have:
NameVirtualHost *:80
NameVirtualHost *:443
Listen 8033
The reason for the listen on 8033 was because I am using OpenVPN's feature to dual use port 80. Once I changed my
<VirtualHost *:80> to
<VirtualHost *:8033>
everything worked.
The curious thing though is why all of my other virtual domains and *.conf files work even though they have
<VirtualHost *:80>
and not
<VirtualHost *:8033>.
Any idea why that would be the case?