htaccess redirect URI to parent directory
Asked Answered
T

1

5

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?

Tetrameter answered 24/1, 2014 at 4:31 Comment(2)
Why can you not move api under /var/www/thesite/web?Due
@Due because web is accessible publicly. you don't want your source code accessible.Tetrameter
S
7

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.

Surrebutter answered 24/1, 2014 at 4:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.