apache2 virtualhost configuration with two subdirectories

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 </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

人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:apache2 virtualhost configuration with two subdirectories