.htaccess通配符子域path

我有我的网站设置像这样:

RewriteCond %{HTTP_HOST} !^www\.example\.com RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC] RewriteRule ^/?$ /user/profile.php?name=%1 [L] 

这样做是如果用户访问: test.example.com ,它将显示文件的内容: example.com/user/profile.php?name=test 。 如果有人前往lol.example.com ,则会显示如下页面: example.com/user/profile.php?name=lol lol.example.com ,但该url与子域保持一致,如lol.example.comlol.example.com

这部分正在工作。


题:

如果我去test.example.com/login ,它会显示我的域根文件。 我怎样才能使它显示/user文件夹中的东西?

例如:

  • test.example.com/login将显示example.com/user/logintest.example.com/register将显示example.com/user/register但URL将保持与子域相同?

  • test.example.com/pathtofile应该得到example.com/user/pathtofile的内容。 “pathtofile”应该是dynamic的。 我只想要path在文件夹/user ,而不是根文件夹。

如果有人前往lol.example.com ,则会显示如下页面: example.com/user/profile.php?name=lol ? lol.example.com

严格来说,它将显示页面: lol.example.com/user/profile.php?name=lol (该子域不会被删除)。 但是,因为(我假设)所有的子域和主域指向文件系统上的相同位置,它的工作原理。

test.example.com/pathtofile应该得到example.com/user/pathtofile的内容

您的原始指令仅重写对文档根目录( test.example.com/ )的请求。 要重写/pathtofile那么你可以在上面的.htaccess之后添加另一个规则块。 例如:

 RewriteCond %{HTTP_HOST} !^www\.example\.com RewriteCond %{HTTP_HOST} [^.]+\.example\.com [NC] RewriteCond %{REQUEST_URI} ^/([^/]+)$ RewriteRule !^user/ /user/%1 [L] 

这将重写所有尚未开始/user/且仅包含单个(非零长度)path段的/pathtofile (如/pathtofile而不是/path/to/file ),并重写到/user/pathtofile

更新:如果你有一个ErrorDocument定义,那么你将需要在你的`.htaccess文件的早些时候发生exception,以防止重写错误文档的子请求。 例如:

 RewriteRule ^404\.php$ - [L]