Configuring subdirectory with apache2 and HTTPS

apache-2.2mod-rewritevirtualhost

I have a domain my.domain.com that points to a Zend Framework 2 application at /path/to/project/public which has a .htaccess and index.php file which handles all requests. At /path/to/project/public/blog, I want to have a standalone site which is basically just an index.php file that should be independent from the rest of the site. This all worked fine until I enabled HTTPS on the server. Below is my configuration.

VirtualHost configuration:

<VirtualHost *:80 *:443>
        ServerName my.domain.com
        DocumentRoot /path/to/project/public

        # SSL configuration
        SSLEngine on
        SSLProtocol all
        SSLCertificateFile /etc/apache2/ssl-certs/publickey.crt
        SSLCertificateKeyFile /etc/apache2/ssl-certs/private.key
        SSLCertificateChainFile /etc/apache2/ssl-certs/intermediate_ca.crt

        <Directory "/path/to/project/public">
                DirectoryIndex index.php
                Options FollowSymLinks Indexes
                AllowOverride All
                Order deny,allow
                allow from All
        </Directory>
</VirtualHost>

/path/to/project/public/.htaccess:

RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

When I try to access http://my.domain.com/blog, my browser gets redirected to the HTTPS version of the page, but nothing is displayed even though the HTTP status code is 200 OK. The rest of the site is working fine with SSL/HTTPS, as far static files in /path/to/project/public/css/something.css, for instance.

I tried various things such as adding a virtual host alias, but nothing worked so far. So, to sum up: I want http://my.domain.com/blog to redirect to https://my.domain.com/blog and execute /path/to/project/public/blog/index.php (or any other files within that subdirectory), independent of the rest of my site. At the same time, I want http://my.domain.com/whatever to redirect to https://my.domain.com/whatever and execute /path/to/project/public/index.php.

Does anyone know how to do this? I am not so good at Apache configs/htaccess, so the simpler the better. Thanks in advance!

Best Answer

try using, aliasmatch this should do as you are using both on same vhost.

AliasMatch ^/blog/      /path/to/project/public/blog/index.php
AliasMatch ^/whatever/      /path/to/project/public/index.php

However, i would say get that isolated: i.e. use ssl.conf for ssl configuration & vhost.conf for non-ssl configuration.

Related Topic