How do I make a subfolder act like a document root

2019-05-29 00:20发布

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.

3条回答
小情绪 Triste *
2楼-- · 2019-05-29 00:53

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

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/~
RewriteRule ^(.*)$ ~username/website/$1 [L]
查看更多
不美不萌又怎样
3楼-- · 2019-05-29 01:16

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
查看更多
Root(大扎)
4楼-- · 2019-05-29 01:19

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)
查看更多
登录 后发表回答