Jean-Marc Dormoy
Jean-Marc Dormoy

Reputation: 129

apache alias and subdirectories

I have the following structure

/var/www/mysite/public/
/var/www/mysite/api/

In both directories, .htaccess is set up for rewriting url as follow :

dev.domain.com/example/ => dev.domain.com/index.php?token=example

dev.domain.com/api/example => dev.domain.com/index.php?token=example

My apache conf looks like this

...
<VirtualHost *:80>
   Servername dev.domain.com
   DocumentRoot /var/www/mysite/public/
   Alias /api/ "/var/www/mysite/api/"
   <Directory "/var/www/mysite/api/">
       Options Indexes FollowSymLinks
   </Directory>
</VirtualHost>
...

dev.domain.com/api/ works fine (it calls www/api/index.php) but dev.domain.com/api/example/ calls the public site (www/public/index.php with the query string token=example).

I thought that the apache directive Alias was redirecting also the subdirectories, which apparently is not the case. Could someone tell me where I am wrong?

Upvotes: 4

Views: 24614

Answers (2)

Jean-Marc Dormoy
Jean-Marc Dormoy

Reputation: 129

So this was a problem of rewriting : the aliased directory should not be in the pattern to match.

Here is the final config : apache config file

...
<VirtualHost *:80>
        ServerName dev.domain.com
        DocumentRoot /var/www/mysite/public/

        Alias /api/ /var/www/mysite/api/
        <Directory /var/www/mysite/api/>
             Options FollowSymLinks -Indexes   
             AllowOverride all
        </Directory>
</VirtualHost>
...

and .htacess file the /api/ directory

Options FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301]

RewriteRule ^(.*)/(.*)/$ /api/index.php?object=$1&collection=$2 [QSA,L] 
RewriteRule ^(.*)/$ /api/index.php?object=$1 [QSA,L]

Thanks @freedev for your time.

Upvotes: 5

freedev
freedev

Reputation: 30027

For mysite/api/ you should use absolute path, please try these rewrites:

RewriteRule ^api/(.*)/(.*)/$ /api/index.php?object=$1&collection=$2 [QSA,L] 

RewriteRule ^api/(.*)/$ /api/index.php?object=$1 [QSA,L]

If anything does not work as expected remember you can debug the rewrite process enabling the rewritelog

RewriteLog "/var/apache/logs/rewrite.log"
RewriteLogLevel 7 

Please take care to use rewritelog configuration only in development environment because this will greatly slow down the server.

Upvotes: 3

Related Questions