Subdomain Redirection

本文介绍如何使用CommunityServer的子域模块隔离特定功能,如论坛或博客,使其看似独立应用。通过修改web.config和SiteUrls.config文件,可以实现URL重定向和链接定制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Community Server is a community oriented web application that provides forums, blog, and other functionality. While this is nice it is often desirable to use its components in such a way that a component is separated from the main community. For instance, if you only want forums on your site, you want to use the software solely for its blogging functionality, or any other desired purpose. The SubdomainModule class of TimothyHumphrey.CommunityServer provides a means to accomplish this.

The SubdomainModule class internally redirects specific urls to specific portions of Community Server; it can also intercept links before they are rendered and change them to appear to be within the subdomain url used to access the page. The net effect is that a portion of Community Server can be isolated from the entire community and appear to be a standalone application while unaffecting the rest of the community.

For example, normally blogs are stored under the blogs/(blog) directory, where (blog) is the blog name. To access a blog named "personal" you would normally use http://server.com/blogs/personal. With the SubdomainModule class you could have a subdomain called "personal" that points to this url and access it with the following url, http://personal.server.com.

Usage

Three files are touched to use the SubdomainModule class.

  1. Edit your application's web.config file and add the following entry to the top of your <httpModules> section.

    <add name="SubdomainModule" type="TimothyHumphrey.CommunityServer.SubdomainModule, TimothyHumphrey.CommunityServer" />

    It is important that this line be at the top, or at the very least above the CommunityServer entry, so that it can take affect before Community Server. Your <httpModules> section should look similar to the following when done.

    <httpModules>
      <add name="SubdomainModule" type="TimothyHumphrey.CommunityServer.SubdomainModule, TimothyHumphrey.CommunityServer" />
      <add name="CommunityServer" type="CommunityServer.CSHttpModule, CommunityServer.Components" />
      ...
    </httpModules>

  2. SiteUrls.config is where you specify the subdomain redirections. The following sample illustrates subdomain redirection.

    <subdomains>
      <subdomain domain="blog.timothyhumphrey.name" path="$weblogs/blog" />
      <subdomain domain="localhost/csweb" path="${reader}" />
    </subdomains>

    Each <subdomain> entry defines a different subdomain redirection; there is no limit to how many there can be. Each entry is compared against the incoming url and if a match is found processing is done. Specifically, the domain attribute of each entry is compared against the incoming url. For example, given the following url, blog.timothyhumphrey.name/archive/category/1003.aspx, the first <subdomain> entry would match it. When a match is found the path attribute of the <subdomain> entry specifies where in the Community Server url hierarchy the request should go. In this case it would go to the location where blogs are kept and then the blog named "blog".

    Notice that the location of the weblogs from the preceding example is specified as a variable, $weblogs. The SubdomainModule uses variables to refer to locations defined within the <locations> node of the SiteUrls.config file. For this reason it is recommended that you place the <subdomains> node near it for easy reference when defining your subdomains.

    Variables begin with a dollar sign $ and are followed be a name which should match a name defined within the <locations> node. The name part of a variable may be surrounded by braces { } as in the second <subdomain> entry in the sample listed earlier.

    Also, notice that in the second example entry the server name, localhost, is followed by a path. This indicates that the subdomain to be matched need not technically be a subdomain, any part of the url up to any query string is checked. However, any subdomain specified should at least include the base url of the location the application is stored. For instance, using the second <subdomain> entry of the sample, the domain attribute must be at least localhost/csweb, and not simply localhost, if the Community Server application is stored under the csweb directory. This normally need not be remembered, but it bears mentioning.

    <location> entries in SiteUrls.config with the exclude attribute set to true will not be redirected should a url matching them be encountered. For instance, the control panel in Community Server is used to make administrative changes and is accessed with the following url from the root Community Server site, /controlpanel. Given the first sample listed earlier this url would be redirected to a blog, preventing access to the control panel if the following were the full url, blog.timothyhumphrey.name/controlpanel.

    As part of the SubdomainModule's processing it removes the subdomain portion of the url, in this case blog.timothyhumphrey.name, and compares the url that remains against <location> entries with their exclude attributes set to true. If a match is found no redirection is performed. So the url blog.timothyhumphrey.name/controlpanel will not be redirected to a blog and so the control panel may be used as normal. In addition to the exclude attribute the SubdomainModule also checks a custom attribute it supports named subdomainExclude. This attribute works exactly as the exclude attribute does but since it is not known to Community Server it provides a means to indicate to the SubdomainModule to exclude a url while still allowing Community Server to include it. Adding additional <location> entries is how you may indicate to the SubdomainModule that certain urls should not be redirected. In fact you may want to add the following entries to enable logging in to Community Server even with redirection in place.

    <location name="login" path="/login.aspx" subdomainExclude="true" />
    <location name="logout" path="/logout.aspx" subdomainExclude="true" />

  3. The communityserver.config file is optionally modified, but recommended. An entry to this file is made so that the links Community Server renders are altered to appear to come from the subdomain. For instance, a link to a blog category might normally appear as /blogs/blog/archive/category/1003.aspx but will get altered to be /archive/category/1003.aspx.

    The entry that is made is a new SiteUrlsDataProvider. It is easiest to search for the occurrence of this phrase in this file to find the entry containing it. You will have to comment out the existing entry provided by Community Server and replace it with the one provided by TimothyHumphrey.CommunityServer, like so:

    <!--
    <add
      name = "SiteUrlsDataProvider"
      type = "CommunityServer.Components.SiteUrlsData, CommunityServer.Components"
      path = "SiteUrls.config"
    />
    -->
    <add
      name = "SiteUrlsDataProvider"
      type = "TimothyHumphrey.CommunityServer.SiteUrlsData, TimothyHumphrey.CommunityServer"
      path = "SiteUrls.config"
    />

The entries in the <subdomains> node of the SiteUrls.config file are read when the Community Server application is started and never again. This helps to speed up the subdomain redirection processing but can make editing of the subdomain configuration more difficult. To overcome this a special url can be used to force the SubdomainModule to re-read the subdomains. Going to /subdomains.axd will cause the SubdomainModule to re-read the subdomains. The slash / character at the front indicates it is referenced from the root of the Community Server url. For instance, if Community Server were installed at localhost/csweb then the following would be the address of the subdomains.axd file, localhost/csweb/subdomains.axd. Similarly, if this were the install location, blog.timothyhumphrey.name, this would be the subdomains.axd url, blog.timothyhumphrey.name/subdomains.axd.

转载于:https://www.cnblogs.com/AlphaWu/archive/2006/08/01/465006.html

### FRP 子域名为空的解决方案 当遇到 `subdomain` 为空的问题时,通常是因为客户端和服务端之间的配置不匹配所引起的。具体来说,在服务端未启用 `subdomain_host` 功能的情况下尝试设置自定义子域可能会引发错误。 为了确保 `subdomain` 正常工作并避免其为空,需遵循以下建议: - **确认服务器端已开启子域名功能** 如果希望使用特定于某个顶级域名下的子域名映射,则应在服务端(`frps`)配置文件中指定该顶级域名作为 `subdomain_host` 参数值[^2]。这一步骤对于后续能够成功解析来自客户端请求中的子域名至关重要。 - **调整客户端配置以适应服务器设定** 客户端 (`frpc`) 应根据实际需求来决定是否要利用 `custom_domains` 或者默认由系统分配随机子域名的方式来进行访问。如果确实需要用到固定的子域名形式,并且已经按照上述方法启用了相应的选项,那么应当保证所提供的名称不会违反任何既定规则——比如成为另一个更大范围内的有效主机名的一部分[^1]。 通过以上措施可有效地处理好关于 `subdomain` 设置方面可能存在的问题,从而实现稳定可靠的内外网通信连接。 ```ini # 示例:服务端 (frps.ini) 配置片段 [common] bind_port = 7000 subdomain_host = your-domain.com # 启用此参数以便支持子域名特性 # 示例:客户端 (frpc.ini) 配置片段之一 - 使用预设子域名 [web] type = http local_ip = 127.0.0.1 local_port = 80 custom_domains = web.your-domain.com # 示例二:如果不特别指明 custom_domains 字段,默认会得到形如 xxx.your-domain.com 这样的地址 [sub_web] type = http local_ip = 127.0.0.1 local_port = 8080 use_compression = true ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值