apache2 virtualhost configuration with two subdirectories - apache

I'm running Apache 2.4 on Ubuntu 14.04 server. It's purpose is a mail server so it has postfix, dovecot and roundcube on it (amongst other things). I'm trying, and failing, to configure Apache to serve the pages that I want.
I have an SSL certificate installed and working correctly. I want to force all access over HTTPS so I have:
<VirtualHost *:80>
Redirect / https://mailserver.hni.ae/
</VirtualHost>
Both sets of files to be served are under /var/www/html, the first being /var/www/html/A and the other /var/www/html/B (let's say). I have configured my /etc/apache2/sites-available/000-default.conf (which has a symlink to ./sites-enabled) to be:
<VirtualHost *:443>
SSLEngine On
SSLCertificateFile /etc/ssl/private/mycert.crt
SSLCertificateKeyFile /etc/ssl/private/mycert.key
ServerAdmin webmaster#mydomain.com
ServerName www.mydomain.com
DocumentRoot /var/www/html/
DirectoryIndex index.php
<Directory /var/www/html/A>
Options FollowSymLinks
AllowOverride None
Order Allow,Deny
Allow from all
</Directory>
<Directory /var/www/html/B>
Options FollowSymLinks
AllowOverride None
Order Deny,Allow
Deny from All
Allow from 192.168.1.1
</Directory>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And that works. I can go to www.mydomain.com/B and it serves the login page for those pages (only when I access from the specified IP address), and www.mydomain.com/A and login to the pages from app A.
My problem: I want to be able to go to www.mydomain.com/C and just plain www.mydomain.com and be redirected to www.mydomain.com/A but when I use Redirect ... the server gets into a loop and tries to serve www.mydomain.com/AAAAAAA.... I suspect I should use the RedirectMatch temp ^/$... directive but can't get that to work either. Maybe something to do with the Redirect for :80 to :443 clashing? There is no .htaccess involved as I'm using AllowOverride None.
I've read the Apache 2.4 documentation thoroughly but just can't figure it out. Any suggestions?

You can use a RewriteRule. Add this to your VirtualHost:
RewriteEngine On
RewriteRule ^/(C/?|)$ /A [R,L]
Make sure mod_rewrite is enabled too.
Explanation:
Regex ^/(C/?|)$ will match /C optionally followed by a /, or just / i.e. the root of www.mydomain.com

Related

Keep original URL after redirection by domain provider

Goal
fharrell.com/* is redirected by the domain provider to hbiostat.org/blog/*. I want to keep the address bar showing fharrell.com/*
Apache2 Setup
/etc/apache2/apache2.conf is standard with the following exception:
<Directory /home/ubuntu/htdocs/>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
/etc/apache2/sites-enabled/hbiostat.org.conf is symbolically linked from /etc/apache2/sites-available/hbiostat.org.conf
hbiostat.org.conf sets the document root as /home/ubuntu/htdocs which has been working well for some time
Contents of hbiostat.org.conf:
<VirtualHost *:443>
ServerAdmin my#email.address
DocumentRoot /home/ubuntu/htdocs
ServerName hbiostat.org
ServerAlias www.hbiostat.org
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /home/ubuntu/htdocs>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory /home/ubuntu/htdocs/blog>
RewriteEngine on
RewriteBase /
RewriteRule ^hbiostat\.org/blog$ fharrell.com [R]
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/hbiostat.org/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hbiostat.org/privkey.pem
</VirtualHost>
<VirtualHost *:80>
ServerName hbiostat.org
ServerAlias www.hbiostat.org
DocumentRoot /home/ubuntu/htdocs
<Directory /home/ubuntu/htdocs/blog>
RewriteEngine on
RewriteBase /
RewriteRule ^hbiostat\.org/blog$ fharrell.com [R]
</Directory>
</VirtualHost>
Systax was checked using sudo apachectl -t.
I checked that mod rewrite is active using sudo a2enmod rewrite and restarted the server with sudo systemctl restart apache2
But this has no effect, with hbiostat.org/blog/* remaining in the addressbar.
Tried:
Many remedies on stackoverflow.com (including the two below) and elsewhere, including putting the commands into an .htaccess file (I'd like to avoid the .htaccess approach).
Any help appreciated.
Redirect domain but keep original url
Redirect subfolder URL but keep original domain name
You can't make the browser display a different domain after a 30x redirect.
mod_rewrite doesn't do what you're thinking it does.

Unable to get Apache docker container to serve virtual host with subdomain

I have a site I currently run that works well, but both to learn and to make it more portable, I've been trying to dockerize it. I'm using the offical apache and php images, and setup my virtual hosts identical to how I have on the running site, just with different domains. Unfortunately, while I can get one to work, the second does not.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName api.gamersplane.local
DocumentRoot /var/www/api
ErrorLog "/var/log/gamersplane.api"
CustomLog "/var/log/gamersplane.api" common
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://api:9000/var/www/$1
ProxyPassReverse ^/(.*\.php(/.*)?)$ fcgi://api:9000/var/www/$1
RewriteEngine On
RewriteBase /
RewriteRule !\.(css|jpg|js|gif|png|ico|eot|woff|ttff|svg|psd)$ dispatch.php
<Directory /var/www/api/>
Options FollowSymLinks
Require all granted
</Directory>
LogLevel notice
</VirtualHost>
<VirtualHost *:80>
ErrorLog "/var/log/gamersplane"
CustomLog "/var/log/gamersplane" common
ProxyPreserveHost On
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://api:9000/var/www/$1
ProxyPassReverse ^/(.*\.php(/.*)?)$ fcgi://api:9000/var/www/$1
ServerName gamersplane.local
ServerAlias *.gamersplane.local
DocumentRoot /var/www
RewriteEngine On
RewriteBase /
RewriteRule !\.(css|jpg|js|gif|png|ico|eot|woff|ttff|svg|psd)$ dispatch.php
<Directory /var/www/>
Options FollowSymLinks
Require all granted
</Directory>
LogLevel notice
</VirtualHost>
Originally, I had the first vhost (api.gamersplane) second, but thought maybe it was the server alias that was the problem. Then I switched positions, and even commented out the base (gamersplane.local), but had no luck. This works on the running site, so I can't figure out what's wrong with it.
Checking the headers returned by Postman, I do see it has the Apache and PHP headers, so it seems to be hitting SOMETHING, I just don't know what.

How can I internally point to a domain but keep the url with the subdomain constant?

I have a domain www.example.com hosted in one location.
I have created another account with a different hosting provider that allows me to create subdomains: www.test1.example.com and www.test2.example.com
I am putting a custom page for each of the subdomains when the user goes to test1.example.com, test2.example.com....
After the user logs in on this custom page, I want to maintain the subdomain (of test1.example.com) but internally have all requests point to www.example.com.
I am running the custom pages on Apache and the domain pages on Apache Tomcat - I think that using 'mod_rewrite' is the way to go?
Put the following in a .htaccess file in your subdomain's document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test1\.example\.com$
RewriteRule ^/(.*) http://example.com/$1 [redirect,last]
To enable mod_rewrite on a modern Ubuntu web server run this command:
sudo ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
Make sure AllowOverride is set to "All" in your VirtualHost configuration (for example /etc/apache2/sites-available/default):
<Directory /var/www/document/root/>
AllowOverride All
</Directory>
then restart Apache:
sudo /etc/init.d/apache2 restart
So, basically the answer I found that worked was to use mod_proxy. I enabled that as an Apache module and included the following in my httpd-vhosts.conf file.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName test1.example.com
DocumentRoot "location_of_the_custom_page"
ErrorLog "logs\errors.log"
<directory "D:\wamp\www\capitalfloat">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Deny from all
Allow from all
</directory>
</VirtualHost>
<VirtualHost *:80>
ServerName localhost2
ServerAlias *.example.com
ErrorLog "logs\errors.log"
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://www.example.com
ProxyPassReverse / http://www.example.com
</VirtualHost>
I also had to include 'http://www.example.com' and 'test1.example.com' in the Windows Host File (For me, C:\Windows\System32\drivers\etc\hosts). In my custom login page, the request goes to 'example.com' and all subsequent requests are sent to 'www.example.com' but the url still shows 'test1.example.com/...'

How to use https?

If on the server, we already setup/configured the SSL certificate, how could I make my websites using secure page? Just make the linke to https://example.com/etc.php?
Thanks!
Two things have to be in place.
You'll need to setup the ssl cert properly, which it sounds like you have
As the other commentator said, this will depend upon which webserver you're using. More likely than not, apache:
Apache:
You'll need to modify the apache settings to support the https version of your site. If you're using a modern installation of Apache2 with virtual hosts, usually there will be a "sites-available" directory where individual config files exists for each domain. For a domain that will have both http and https (80 and 443), you would do something like this, assuming apache is listening on 127.0.0.1 (this would not be the case for most apache installations, so be sure to change the ip). It also goes without saying that you need to change the paths and domain name in the following:
<VirtualHost 127.0.0.1:80>
ServerAdmin somebody#domain.com
ServerName somebody.com
ServerAlias www.somebody.com
DocumentRoot /home/somebody/www
<Directory "/home/somebody/www">
Options FollowSymLinks
AllowOverride All
Options -Indexes
</Directory>
ErrorLog /home/logs/somebody.error.log
CustomLog /home/logs/somebody.access.log combined
</VirtualHost>
<VirtualHost 127.0.0.1:443>
SSLEngine On
SSLCertificateFile /etc/apache2/ssl/something.crt
SSLCertificateKeyFile /etc/apache2/ssl/something.key
SSLCertificateChainFile /etc/apache2/ssl/gd_bundle.crt
ServerAdmin somebody#something.com
ServerName somebody.com
ServerAlias www.somebody.com
DocumentRoot /home/somebody/www
<Directory "/home/somebody/www">
Options FollowSymLinks
AllowOverride All
Options -Indexes
</Directory>
ErrorLog /home/logs/somebody.ssl.error.log
CustomLog /home/logs/somebody.ssl.access.log combined
</VirtualHost>
If you are using nginx, there is a similar dual block you'll need to have for :80 and :443. Look at the block you already have for 80 and consult their documentation:
http://nginx.org/en/docs/http/configuring_https_servers.html
You may also be using iis, in which case, here are the instructions for version 7:
How do I configure a site in IIS 7 for SSL?

accessing site over https causes folder structure to be shown in browser, not application

I have a rails 3 app running with following stack:
ubuntu lucid lynx, apache2, passenger hosted on Amazon EC2
When accessed over http, the pages load and the site has no problems at all.
Then i installed a valid SSL cert on the webserver. When i try to access the same url over https, and it looks like passenger is not being activated. instead, in the browser, the folder structure of the rails application is rendered.
What configuration changes or modifications do I need to make to Apache and/or Passenger so that the https URL returns the same thing as the corresponding http url?
You need something like this:
<VirtualHost *:80>
ServerName orders.xxx.com
DocumentRoot "/var/www/html/ps-main/current/public"
CustomLog /var/log/apache2/orders common
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://orders.xxx.com%{REQUEST_URI}
NameVirtualHost *:443
RailsEnv production
<Directory /var/www/html/ps-main>
AllowOverride all
Options -MultiViews
</Directory>
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
CustomLog /var/log/apache2/orders common
ServerAdmin webmaster#xxx.com.com
ServerName orders.xxx.com
SSLCertificateFile /etc/ssl/cert.crt
SSLCertificateKeyFile /etc/ssl/server.key
SSLCertificateChainFile /etc/ssl/intermediate.pem
# SSLProtocol all
SSLCipherSuite HIGH:MEDIUM
DocumentRoot /var/www/html/ps-main/current/public
<Directory /var/www/html/ps-main/current/public>
Options -MultiViews
AllowOverride all
</Directory>
</VirtualHost>
This will redirect all http traffic to https. If that's not what you need, remove the redirects.