htaccess redirect URI to parent directory

maxwell2022 picture maxwell2022 · Jan 24, 2014 · Viewed 8.9k times · Source

I have a website hosted in /var/www/thesite (apache server), it's a Symfony2 website so inside this folder I have a web folder to which my virtual host is pointing.

So my virtualhost is www.mysite.com -> /var/www/thesite/web

In this web folder I have a .htaccess to format URL nicely.

Now, I've added an API in /var/www/thesite/api, the API is implemented using Silex. I want to redirect all request http://www.mysite.com/api to this new framework.

So I've added in /var/www/thesite/web/.htaccess

RewriteCond %{REQUEST_URI} ^/api
RewriteRule ^api(.*)$ ../api/web/index.php [NC,QSA,L]

But I get:

Bad Request

Your browser sent a request that this server could not understand.

I'm not sure if I can access parent folder in the .htaccess. I don't want to change my virtualhost target directory to avoid security breach.

How can I solve this issue?

Answer

Jon Lin picture Jon Lin · Jan 24, 2014

You can't route a request to outside of the site's document root, which is /var/www/thesite/web. So you can't access /var/www/thesite/ or /var/www/thesite/api from inside the /var/www/thesite/web directory. The 400 Bad request is because of the ../api/ bit of your rule's target.

Something you can try doing is just using php to include/require the api's index.php:

RewriteRule ^api/(.*)$ /index_api.php [L]

And in the index_api.php you can include or require the "../api/web/index.php" file.