Htaccess rewrite to file located up one level

Posted on

Htaccess rewrite to file located up one level – Problems with loading a website are often blamed on the Internet connection, but even the most perfectly set up network cannot help if there is no service to reply at your destination. One of the most popular HTTP servers used for this task is Apache2. Much of Apache’s popularity can be attributed to its easy installation and use, but never the less it is possible to run into problems with even the easiest of the software. If you’ve encountered an issue loading your web page, follow these simple troubleshooting methods outlined in this guide to attempt to get your web server back up and working again. Below are some tips in manage your apache2 server when you find problem about apache-2.2, apache-2.4, .htaccess, mod-rewrite, rewrite.

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?

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]

Leave a Reply

Your email address will not be published.