.htaccess重定向到公用文件夹,不包括子域

时间:2019-09-10 14:45:26

标签: .htaccess redirect directory subdomain

我有一个PHP MVC网站,需要将根目录重定向到公用文件夹。

www文件夹包括:

- app
- public
- subdom/subdomain

子域文件夹为 subdomain.website.com

app文件夹受 Options -Indexes

限制

我需要重定向到公用文件夹,但是如果我正在访问子域,则不需要重定向。

RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

这将始终使我进入公用文件夹,但无法访问我的子域。

我总是可以访问我的子域,也可以重定向到公用文件夹,即使我想访问我的子域。

这是Wedos虚拟主机www文件夹中的默认.htaccess文件。

RewriteEngine On

RewriteCond %{REQUEST_URI} !^domains/
RewriteCond %{REQUEST_URI} !^/domains/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{DOCUMENT_ROOT}/domains/%2 -d
RewriteRule (.*) domains/%2/$1 [DPI]

RewriteCond %{REQUEST_URI} !^subdom/
RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^subdom/[^/]+/(.+[^/])$ /$1/ [R]

如何从重定向中排除子域?

website.com 应该是 website.com/public

subdomain.website.com 应该保持这种方式-不应为 website.com/public

谢谢!

1 个答案:

答案 0 :(得分:1)

您应在处理RewriteCond的2条规则之前添加/public,以检查当前主机名:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?website\.com$ [NC]
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?website\.com$ [NC]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
相关问题