apache alias and subdirectories

20,910

Solution 1

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.

Solution 2

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.

Share:
20,910
Jean-Marc Dormoy
Author by

Jean-Marc Dormoy

Updated on July 09, 2022

Comments

  • Jean-Marc Dormoy
    Jean-Marc Dormoy almost 2 years

    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?

  • Jean-Marc Dormoy
    Jean-Marc Dormoy over 11 years
    Well... it still not works. If I try to access to domain.com/api/user/ (should be rewritten to /api/index.php?object=user) I have the following error in the log [error] [client 10.10.0.6] File does not exist: /var/www/domain.com/api/user. Btw, the apache server is behind a NGINX reverse proxy (but it works perfectly for all rewritings except with the alias)
  • freedev
    freedev over 11 years
    I suggest to try this rewrite in a isolated environment (for example you computer) and after copy it into your server. This could help you to debug correctly the request/response process.
  • Jean-Marc Dormoy
    Jean-Marc Dormoy over 11 years
    Thank you... reading the rewrite.log gave me the solution. The right rewriting rule is : RewriteRule ^(.*)/$ /api/index.php?object=$1 [QSA,L]
  • freedev
    freedev over 11 years
    you welcome :), so if I understood well, the first part of url, in your case, will be stripped under mysite/api/
  • Jean-Marc Dormoy
    Jean-Marc Dormoy over 11 years
    yes. Sounds this is the behavior of the Alias. But honestly, I didn't investigate further, now that it works ;)