10006

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

Answer1:

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

Recommend

  • Is the web.config more secure than the database?
  • how to add 3d models dynamically in glsurface view renderer in android
  • Bulk mailing using SMTP server in ASP.Net
  • How can I do a Query/Retrieve in JAVA?
  • How to change color of icons in Font Awesome 5?
  • Problem getting terminal output from ImageMagick's compare.exe ( Either by pipe or Python )
  • SQLAlchemy : Column name on union_all
  • Matlab: How to calculate how many unique strings are stored in the cell?
  • Java last added button allows agent.move, rest dont
  • How to have the correct execution from a batch script?
  • apache2 virtualhost configuration with two subdirectories
  • How do I avoid redefining the parent element class in CSS
  • Can't load installed PHP version in Apache (Homebrew OS X)
  • Debian Jessie - Apache2 / PHP 5.6, can't upload more than 128kb
  • 500 internal server error in ruby on rails
  • How to Use Two Different PHP Versions on One Server
  • Azure Diagnostic is not saving logs in azure tables
  • Linux command line : edit hacked index files
  • Cordova Apache wrong module path
  • How do you keep a running instance for Google App Engine
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • Why are YouTube videos using 'youtube.com/v' not loading
  • Prevent Tomcat from caching request during starup
  • Web.config system.webserver errors
  • Optimizing database types to compact database (SQLite)
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Cross-Platform Protobuf Serialization
  • Modifying destination and filename of gulp-svg-sprite
  • Do I've to free mysql result after storing it?
  • GridView Sorting works once only
  • How to get icons for entities from eclipse?
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • using HTMLImports.whenReady not working in chrome
  • Turn off referential integrity in Derby? is it possible?
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • JaxB to read class hierarchy