如何动态地从子域获取域名



我有以下正在工作的htaccess:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www.)([^.]+).([^.]+).([^.]+)$ [NC]
RewriteRule ^ %1%{REQUEST_URI} [L]

我有 4 个指向同一服务器的域 (domain.com(

example1.com
example2.com
example3.com
example4.com

现在当我去的时候有实际的htaccess

test.example1.com

它将显示目录中的文件

domain.com/test

正是我想要的。

但是我需要实现一个新功能。

所以当我去

test.example1.com

它需要去

domain.com/example1.com/test

同样适用于

test2.example1.com ---->  domain.com/example1.com/test2
test5.example2.com ---->  domain.com/example2.com/test5
test6.example3.com ---->  domain.com/example3.com/test6
test9.example4.com ---->  domain.com/example4.com/test9
它不会重定向,

它会重写,我不希望它重定向。

您可以将现有规则替换为以下内容:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www.)([^.]+).([^.]+.[^.]+)$ [NC]
RewriteRule ^ %2/%1%{REQUEST_URI} [L]

最新更新