【问题标题】:Pointing subdomains to a "directory"将子域指向“目录”
【发布时间】:2014-01-28 13:01:12
【问题描述】:

关于将子域指向目录,我有两个问题:

  1. 目前我在本地运行,但我可以在我设置的假域上运行我的站点(使用 hosts 文件),它名为 mysite.com。我怎么能(通过服务器设置?)这样做所有子域都指向 / ?示例 anders.mysite.com 也应显示 mysite.com 和 asdassdd.mysite.com。

  2. 也许 1. 没有必要,但是我如何通过 htaccess 点 anders.mysite.com 到 mysite.com/anders ?重要的通知是不应该重定向。

为什么我想到 1. 是因为我不想在 htaccess 或任何 apache/domain 设置中的任何地方指定子域是什么,因为这将是动态的(由我的 web 应用程序中的登录用户创建)

目前用户可以使用 mysite.com/anders/ 这是他们创建的 URI,而不是真正的目录。然后在 Kohana 引导程序中,我抓取 URI 并显示相关的用户页面。

我正在使用 Kohana MVC 框架,并且在我的 htaccess 中有这个:

# Turn on URL rewriting
RewriteEngine On

# Installation directory
RewriteBase /


# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>


# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

任何帮助表示赞赏!

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    how do i by htaccess point anders.mysite.com to mysite.com/anders ? Important notice is that should not redirect.

    你可以使用这条规则:

    RewriteCond %{HTTP_HOST}:%{REQUEST_URI} ^([^.]+)\.[^:]+:(?!/\1/).*$
    RewriteRule ^ /%1%{REQUEST_URI} [L]
    

    【讨论】:

      【解决方案2】:

      对于 1,在您的虚拟主机中添加一个 ServerAlias

      ServerAlias *.mysite.com
      

      然后任何子域(包括 www)都将指向同一个文档根目录。

      如果您想将子域放在它们自己的目录中(并且由于您可以访问服务器配置),您可以使用 mod_vhost_aliasVirtualDocumentRoot 指令:

      VirtualDocumentRoot /path/to/your/htdocs/%1
      

      因此,如果您请求 blah.mysite.com,您最终将在 /path/to/your/htdocs/blah 作为文档根。

      如果您需要 URI 中不存在的目录的目录名称以便 Kohana 可以路由它们,则需要确保已加载 mod_proxy:

      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteCond %{HTTP_HOST} ^([^.]+)\.
      RewriteCond %{REQUEST_URI} !^/%1/
      RewriteRule ^(.*)$ /%1/$1 [L,P]
      

      使用正确的 URI 将请求代理回 Kohana。

      【讨论】:

      • 我的主机文件有 127.0.0.1 mysite.com 和我的 httpd-vhosts.conf 现在有: ServerAlias *.mysite.com 但我仍然无法到达 blabla.mysite.com 上的文档根目录。我错过了什么吗?
      • 澄清一下,我的需要是 Kohana 应该像 mysite.com/blah/page/stuff 一样阅读 blah.mysite.com/page/stuff
      • 因为我不在 mysite.com 上,所以我使用 hosts 文件让浏览器认为我在。
      • @Karem 你在使用代理规则吗?在你的 htaccess 文件中? (并加载了 mod_proxy)
      • 我的 htaccess 看起来仍然与您在问题中看到的完全一样。我现在刚刚在服务器上激活了 apache 的模块代理。我现在该怎么办?
      猜你喜欢
      • 1970-01-01
      • 2011-08-05
      • 2019-04-29
      • 2014-02-25
      • 2016-07-21
      • 2014-10-30
      • 2013-12-29
      • 1970-01-01
      • 2015-05-08
      相关资源
      最近更新 更多