How do I make a subfolder act like a document root using htaccess?

我的未来我决定 提交于 2019-12-07 19:12:16

问题


Site location: http://localhost/~username/website

The website loads lots of images with an absolute url such as /image/image.png.

I need that request to go to: http://localhost/~username/website/image/image.png instead of http://localhost/image/image.png.

I also need this to not affect any other folders or the root folder. So that I could also access http://localhost/image/image.png if I wanted to.

Is there some way of making it so that when it's requested from this subfolder to redirect?

I want the absolute reference like /css/something.css and /image/image.png to points to /subdirectory/css/something.css and /subdirectory/image/image.png. That way I don't have to rewrite all the absolute references. So, I don't want to modify the root directory.

I'm wondering if setting up a virtual host that would not allow the subdirectory "website" to have no ability to access the root. I don't ever need root access from this folder.


回答1:


Be sure you are allowed to use .htaccess file in your webserver. Look at this how to enable in apache: https://www.digitalocean.com/community/tutorials/how-to-use-the-htaccess-file

Create a .htaccess file in root or ~username/website directory Then write this to your .htaccess file:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^~username/web/image/(.*) /image/$1



回答2:


Why the requirement to use /image/image.png? Why wouldn't you just use one of these instead:

http://localhost/~username/website/image/image.png (absolute)
image/image.png (relative without preceding slash)



回答3:


You can use this rule in root .htaccess OR in Apache/vhost config:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/~
RewriteRule ^(.*)$ ~username/website/$1 [L]


来源:https://stackoverflow.com/questions/35782032/how-do-i-make-a-subfolder-act-like-a-document-root-using-htaccess

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