找到你要的答案

Q:.htaccess redirect root to folder

Q:.htaccess重定向根文件夹

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:

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. Following an example for illustration:


The html file /www/folder/index.html contains an image:

<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. Correct would have been one.com/folder/assets/img.jpg (one notice the folder in the path). Obviously, if the user calls two.com/index.html the problem doesn't exist.


Therefore, I thought I would change the root (base directory) of website two via .htaccess mod rewrite depending on which URL gets called. 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:

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!

我有两个独立的网站在我的服务器。其中一个网站是位于另一个子文件夹。这两个网站可以访问两个不同的领域。以下是我服务器上的文件夹结构:

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

因此,如果我想访问网站2我可以这样做以下网址:

one.com/folder
two.com

我的问题:使用绝对路径包含文件(CSS、JS / IMG)的网站,我需要适应我的根URL的用户访问不同的网站。下面举例说明:


HTML文件/网络/文件夹/文件包含一个图像:

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

如果用户访问这个HTML文件通过领域one.com/folder/index.html图像源路径被翻译成one.com/assets/img.jpg那里找不到任何图像。正确的就one.com/folder/assets/img.jpg(一个通知路径中的文件夹)。显然,如果用户电话two.com/index.html问题不存在。


因此,我想我会改变根(根目录)网站两通过.htaccess mod rewrite根据URL被称为。下面简单的伪代码我想实现:

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

以下我目前我的.htaccess文件:

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

不幸的是,我好像错过了一些东西。到目前为止,它不工作。任何建议都不胜感激!

answer1: 回答1:

You can place this rule in /www/.htaccess folder:

RewriteEngine On

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

你可以把这条规则/网络/ .htaccess文件夹:

RewriteEngine On

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