【问题标题】:Subdomain points to subdirectory dynamically through htaccess without changing URL子域通过htaccess动态指向子目录而不改变URL
【发布时间】:2016-05-31 05:58:21
【问题描述】:

我想通过 htaccess 动态地将子域指向子目录,而不更改下面显示的 url 和目录列表。

|-- Test (Root Directory) <-- test.com redirects to this folder.
|-- index.php
|
|-- subdomain (Directory)
|
|---- abc (Directory) <-- abc.test.com redirects to this folder.
|------ index.php
|
|---- pqr (Directory) <-- pqr.test.com redirects to this folder.
|------ index.php
|
|---- xyz (Directory) <-- xyz.test.com redirects to this folder.
|------ index.php

我已将以下重写规则应用于点子域。

.htaccess 文件

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.test.com
RewriteRule (.*) http://test.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^test\.com $
RewriteCond %{REQUEST_URI} !^/subdomain/
RewriteRule (.*) /subdomain/$1

RewriteCond %{HTTP_HOST} ^(^.*)\.test.com
RewriteCond %{REQUEST_URI} !^/subdomain/
RewriteRule (.*) /subdomain/$1

我在 htaccess 重写方面不太擅长。

我已经关注这个subdomain on the fly

但我没能成功。非常感谢您的建议。

谢谢。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    希望这会有所帮助

    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^www.test.com
    RewriteRule (.*) http://test.com/$1 [R=301,L]
    
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteCond %{HTTP_HOST} ^([^\.]+)\.test\.com$ [NC]
    RewriteRule ^(.*)$ http://test.com/subdomain/%1/$1 [L,NC,QSA]
    

    【讨论】:

    • 感谢您的回复。但我想在不更改任何网址的情况下做到这一点。例如test.com 指向根目录,abc.test.com 指向 abc(sub-folder) 但不改变 url。
    • 您的域是通配符域吗?如果没有,您需要在您的 DNS 服务器上进行设置
    • 我在我的 DNS 服务器上设置了 *.test.com。
    猜你喜欢
    • 1970-01-01
    • 2016-06-26
    • 2014-01-17
    • 2015-04-03
    • 1970-01-01
    • 2011-08-05
    • 2017-07-23
    • 1970-01-01
    • 2016-09-24
    相关资源
    最近更新 更多