将所有请求重定向到子域到适当的文件夹

时间:2018-02-12 22:57:00

标签: .htaccess mod-rewrite apache2

我有多个客户端,我有子域名 client1.example.com client2.example.com ...等等

现在在我的根目录中,我有一个共同的代码库,其余因客户端而异

所以在我的根目录中我有这些文件夹  mymainrepo client1 client2

mymainrepo包含所有常用代码

client1包含多个文件夹,如缓存dbsettings client2包含多个文件夹,如缓存dbsettings

但是每个客户端的缓存和dbsettings的内容都不同。 所以我想要做的是,如果客户端发出请求并要求提供缓存文件夹contenet,我希望它从客户端文件夹中提供

所以,如果请求是client1.example.com/cache 它将提供来自

的内容
doc root
    |
    |
     -------client1
                |
                |_______cache

如果请求是client2.example.com/cache 它将来自

doc root
        |
        |
         -------client2
                    |
                    |_______cache

依旧等等。

任何帮助将不胜感激 感谢

**已编辑**

好的,所以我试过这个,这种方式起作用

RewriteEngine on
RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.domain.com$[nc]
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [r=301,nc]
DirectoryIndex client1/index.html

问题是我想要这个动态而不是client1硬编码

DirectoryIndex client1/index.html

我试过

DirectoryIndex%1 / index.html

但这不起作用

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容

RewriteEngine on


RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.domain.com$
RewriteRule ^.*$ /%1%{REQUEST_URI} [L]

这会将client.domain.com/path重写为主文档根目录的/client文件夹。因此,如果请求为client.domain.com/cache,则会在内部将其重定向到/client/cache,向您显示该文件夹中的内容。

相关问题