Search code examples
.htaccessmod-rewritehttp-redirecturl-rewritingapache2

RewriteRule to an absolute path?


I'm trying to make RewriteRule redirect to an absolute path on the server, outside of the current directory. This works when correctly implemented in PHP (i.e. no privilege issue), but with .htaccess, it doesn't:

RewriteEngine On
RewriteRule ^files/(.*)$ /var/www/files/html/$2

This is an example, though. Imagine this .htaccess file is at /var/www/downloads/html and redirects to the other location. Like I said, it's an example and doesn't have to make sense in this case.

This probably doesn't work because it redirects to /var/www/files/html/var/www/downloads/html, I think.

Question: How do I make RewriteRule point to a directory outside of the current one?

Update: The VirtualHost for this site is:

<VirtualHost *:80>
    ServerName subdomain.domain.com
    DocumentRoot /var/www/files/html
</VirtualHost>

Solution

  • You can change DocumentRoot to /var/www and place your .htaccess there. The rule could then look like

    RewriteRule ^files/(.*)$ /downloads/html/$1
    

    But then, everything below /var/www will be publicly accessible. If this is not an option (e.g. security concerns), you must stick to your PHP solution.