简体   繁体   English

.htaccess将根目录重定向到文件夹

[英].htaccess redirect root to folder

I have two independend websites on my server. 我的服务器上有两个独立网站。 Of which one website is located in a subfolder of the other. 其中一个网站位于另一个网站的子文件夹中。 Both websites are accessible over two different domains. 可以通过两个不同的域访问两个网站。 Following the folder structure on my server: 遵循我服务器上的文件夹结构:

/www                <- one.com
    /index.html
    /assets
        ...
    /folder         <- two.com
        /.htaccess
        /index.html
        /assets
            ...

Therefore, if I want to access website two I can do so with the following URLs: 因此,如果要访问two网站,可以使用以下URL:

one.com/folder
two.com

My problem: Using absolute paths to include files (css/js/img) in website two , I need to adapt my root depending over which URL the user access website two . 我的问题:使用绝对路径在网站two包含文件(css / js / img),我需要根据用户访问网站two URL调整根目录。 Following an example for illustration: 下面以一个示例为例:


The html file /www/folder/index.html contains an image: html文件/www/folder/index.html包含图片:

<img src="/assets/img.jpg" />

If the user access this html file via the domain one.com/folder/index.html the image source path gets translated to one.com/assets/img.jpg where it won't find any image. 如果用户通过域one.com/folder/index.html访问此html文件,则图像源路径将转换为one.com/assets/img.jpg ,在此找不到任何图像。 Correct would have been one.com/folder/assets/img.jpg (one notice the folder in the path). 正确的应该是one.com/folder/assets/img.jpg (请注意路径中的folder )。 Obviously, if the user calls two.com/index.html the problem doesn't exist. 显然,如果用户调用two.com/index.html ,则该问题不存在。


Therefore, I thought I would change the root (base directory) of website two via .htaccess mod rewrite depending on which URL gets called. 因此,我想我会改变网站的根目录(基本目录) two通过.htaccess国防部重写取决于哪个URL被调用。 Following the simple pseudo code I would like to implement: 按照我想实现的简单伪代码:

if url == one.com/folder*
    root = /folder/
else
    root = /

And following what I got so far for my .htaccess file: 并遵循到目前为止我的.htaccess文件的内容:

RewriteEngine on
RewriteCond   %{HTTP_HOST}   ^(www\.)?one\.com
RewriteRule   ^/(.*)$  /folder/$1  [R]

Unfortunately, it seems as if I am missing something here. 不幸的是,似乎我在这里缺少什么。 So far it doesn't work. 到目前为止,它不起作用。 Any advice is much appreciated! 任何建议深表感谢!

You can place this rule in /www/.htaccess folder: 您可以将此规则放在/www/.htaccess文件夹中:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(assets/.+)$ /folder/$1 [L,NC,R=301]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM