子域到子文件夹映射

时间:2021-07-03 08:16:29

标签: php apache .htaccess

例如我的子域是 abc.example.com

我的文件夹是 /htdocs/abcfiles

我的要求是当我访问 url http://abc.example.com 时。 它将显示来自“abcfiles/”文件夹的网页。

我的 .htaccess 文件

RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc.example.com$
RewriteCond %{REQUEST_URI} !^/abcfiles/
RewriteRule (.*) /abcfiles/$1 [L]
RewriteBase /abcfiles/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /abcfiles/index.php [L]

它可以工作,但在某些链接中显示“/abcfiles/”。即 abc.example.com/abcfiles/...

1 个答案:

答案 0 :(得分:2)

您可以在站点根目录 .htaccess 中使用此代码:

RewriteEngine On

# if /abcfiles/ is in URL then remove it
RewriteCond %{THE_REQUEST} \s/abcfiles/(\S*) [NC]
RewriteRule ^ /%1 [L,NE,R=301]

# if hostname starts with abc. then route the request to abcfiles/
RewriteCond %{HTTP_HOST} ^abc\. [NC]
RewriteCond %{REQUEST_URI} !^/abcfiles/ [NC]
RewriteRule (.*) abcfiles/$1 [L]

abcfiles/.htaccess 中有此代码:

RewriteEngine On

RewriteRule ^index\.php$ - [L,NC]

# route every request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]