mod_alias and mod_rewrite don't work together

490 Views Asked by At

I know there are many similar questions on here but unfortunately none of them solve the issue I am having. I am trying to work out why rewrite rules don't work when you also have an alias, below is an explanation of the situation that is causing the problem.

I have many Zend Framework applications that I want to include on the same subdomain as I have limited subdomains I can use and also as I want to create a site for all the projects I have done as demonstrations/tutorials it makes sense to have them all in one place. Like with many web applications/MVC solutions Zend Framework is designed for each application to be on its own domain or sub domain. Therefore I am looking for a solution that makes a folder a site/web application just like it was a sub domain. This is what I have at the moment.

I have the subdomain set up for the demos at: C:/Apache2/htdocs/demos with a public folder within it as the document root (zend framework uses public folders as the document root with other folders above it like application that make the application work; this is for security reasons to prevent users accessing files they shouldn't). I also have C:/Apache2/htdocs/zf-basic-project for the application I want to have as a folder within the demos.example.com sub domain (again it has a public folder as the document root).

The virtual host for the demos subdomain looks like so:

<VirtualHost *:80>
    ServerName demos.example.com
    DocumentRoot "C:/Apache2/htdocs/demos/public"
    ErrorLog "logs/demos-error.log"
    CustomLog "logs/demos.log" common 
    <Directory "C:/Apache2/htdocs/demos/public">
        DirectoryIndex index.php
        AllowOverride All 
    </Directory>

    Alias /zf-basic-project "C:/Apache2/htdocs/zf-basic-project/public/"
    <Directory "C:/Apache2/htdocs/zf-basic-project/public">     
        DirectoryIndex index.php
        AllowOverride None
        Require all granted

        RewriteEngine On
        RewriteBase /zf-basic-project
        RewriteCond %{REQUEST_URI} !^/zf-basic-project [NC]
        RewriteRule ^(/?)(.*) /zf-basic-project/$2 [PT]

        RewriteCond %{REQUEST_FILENAME} -s [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^.*$ /zf-basic-project/index.php [NC,L]
    </Directory>
</VirtualHost>

This half does what I want; the alias makes the zf-basic-project appear in demos.example.com/zf-basic-project as it refers to the public folder of the zf-basic-project projects folder then the code uses files in the folder level above to find all the files it needs to use. It is like it really is in C:/Apache2/htdocs/zf-basic-project/public. The directory section then sets the permissions for using the files and I chose to put the rewrite rules in the directory and not allow .htaccess files so that I do not have to adjust the projects .htaccess file to add the rewrite rules (I want to keep the original project unchanged).

The problem I am having is because zf-basic-project was written to be in its own subdomain it refers to things like CSS via absolute paths e.g. /css/default.css and now that it is a folder within the demos subdomain as far as the browser knows this is actually at /zf-basic-project/css/default.css and therefore I tried to make the rewrite in the directory say /zf-basic-project/css is actually /css which is the code:

RewriteCond %{REQUEST_URI} !^/zf-basic-project [NC]
RewriteRule ^(/?)(.*) /zf-basic-project/$2 [PT]

This says if a link does not start with /zf-basic-project then add it to the URL. It works on normal projects but appears to clash with the alias and doesn't work. I have also added a RewriteBase for good measure which also doesn't seem to do anything. The rest of the rewrites are working as the application runs within the folder but it is missing CSS, JavaScript etc as the links are wrong so does anyone know of a fix to get the rewrite rules to work i.e. /css is treated as /zf-basic-project/css.

I could of course redo all the links to CSS etc but I don't want to do this as I want to keep the project exactly like it was originally as it is a demonstration and ordinarily you wouldn't be in a sub folder so to make things clearer it is nicer to keep things as unmodified as possible.

Thanks in advance for any help, I am most grateful, I must have spent at least 10 hours trying to fix it and can't seem to find the solution.

Ps. For those curious among you the Zend Framework project I want to include in the demo subdomain along with others is on GitHub at https://github.com/paulalbinson/zf-basic-project

1

There are 1 best solutions below

5
On

As far as I can tell, your condition for the rewrite can never be true.

First you are asking to rewrite when the URL is under /zf-basic-project:

RewriteBase /zf-basic-project

Then you ask to match anything not starting with /zf-basic-project (which has just violated the above, and will therefore not match):

RewriteCond %{REQUEST_URI} !^/zf-basic-project [NC]

Meaning this...

RewriteRule ^(/?)(.*) /zf-basic-project/$2 [PT]

...cannot happen, because if the URL didn't start with /zf-basic-project, it is assumed to be part of the "upper" project, i.e. the one inside of C:/Apache2/htdocs/demos/public.

I could be wrong (and someone will no doubt correct me if so), but that's how it looks to me.