apache2 virtualhost configuration with two subdirectories

狂风中的少年 提交于 2020-01-04 09:13:49

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/25408755/apache2-virtualhost-configuration-with-two-subdirectories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!