htaccess redirect URI to parent directory

喜欢而已 提交于 2019-12-07 07:09:29

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/21324491/htaccess-redirect-uri-to-parent-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!