htaccess重定向子域以更正文件夹

时间:2017-03-23 14:47:24

标签: .htaccess

您好我需要为文档根子文件夹中的每个子域显示不同的内容

Read data from folder with subdomain name:

uu-u.domena.com  -> uu-u.domena.com/uu-u/index.php
xxx.domena.com  -> xxx.domena.com/xxx/index.php
doom.domena.com  -> doom.domena.com/doom/index.php

BUT i dont want to show folder name (show content from correct folder)

doom.domena.com/doom/index.php -> doom.domena.com/index.php
xox.domena.com/xox/index.php -> xox.domena.com/index.php

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(.+)\.boo\.loc$   [NC] 
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^$ http://%1.boo.loc/%1/  [L,NC,QSA]

2 个答案:

答案 0 :(得分:0)

规则已指向正确的方向。它捕获子域部分并在目标URL %1中使用它。

需要改进的地方

  • 标记太多,不需要(只有L标志)
  • 附加请求的路径(%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^(.+)\.boo\.loc$ [NC]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ /%1%{REQUEST_URI} [L]

答案 1 :(得分:0)

这对我有用:

# redirect subdomain to subdomain folder in *.domain.com document root
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(.+)\.boo\.loc$ [NC]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^([^/]*)$ /%1%{REQUEST_URI}?folder=%1 [L,QSA]

感谢您的帮助。