Htaccess rewrite to file located up one level

.htaccessapache-2.2apache-2.4mod-rewriterewrite

I have an htaccess in a subdomain with the following rule:

RewriteEngine On
RewriteBase /
RewriteRule ^feed([0-9]+)$ ../image.php?id=$1 [L]

So basically I want to redirect requests from feed which is located in a subdomain to image.php file which is located in the main domain (just one directory above). Using ../ doesn't seem to work like in PHP.

So how can I rewrite to file in the parent directory?

Best Answer

By the sounds of it, you are already in the document root of your subdomain, which is probably defined in its own virtual host (which happens to point to a subdirectory off the main domains document root).

Unfortunately, you can't internally rewrite above/outside the document root using mod_rewrite in .htaccess. In .htaccess the RewriteRule substitution takes a URL-path only, not a filesystem path. However, you can do this if you are using mod_rewrite directly in the server config. In the server config you can specify a filesystem path as the substitution (you would need to explicitly include the full path, you can't use a relative path like ../image.php).

Alternatively, in .htaccess you might be able to proxy the request from your subdomain to the maindomain using mod_proxy with mod_rewrite. However, this might still require some server configuration. For example:

RewriteRule ^feed([0-9]+)$ http://maindomain.com/image.php?id=$1 [P]