Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 274
Jean-Marc Dormoy Avatar asked Feb 18 '23 08:02

Jean-Marc Dormoy


1 Answers

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.

like image 178
Jean-Marc Dormoy Avatar answered Mar 17 '23 06:03

Jean-Marc Dormoy