【问题标题】:subpages in subdomain子域中的子页面
【发布时间】:2011-04-11 10:28:31
【问题描述】:

好的,目的是为我们的会员动态创建无限数量的子域。 因此,对 www.example.com/sub_index.php?user=demo 的请求将转换为 demo.example.com url,而 www.example.com 由 index.php 提供服务。

我已启用名称服务器和 Web 服务器以接受通配符请求(实际上我联系了我的托管服务器来执行此操作)。

在 htaccess 部分我有以下内容:

####rewrite example.com into www.example.com
RewriteCond %{HTTP_HOST} ^example.com 
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.htm([#?][^\ ]*)?\ HTTP/ 
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L] 

####subdomains 
RewriteCond %{HTTP_HOST} !^www\.example.com 
RewriteCond %{HTTP_HOST} ([a-zA-Z0-9]+)\.example.com 
RewriteRule ^(.*)$ sub_index.php?user=%1 

因此,对 demo.example.com/ 和 demo.example.com/index.php 的请求由 www.example.com/sub_index.php?user=demo 提供服务

我的问题是我找不到让子页面在子域中工作的方法 例如:demo.example.com/somepage.php 应该由 www.example.com/sub_page.php?user=demo 提供服务 而 demo.example.com/otherpage.php?id=5 应该由 www.example.com/sub_page2.php?user=demo&id=5 提供服务。

感谢您的任何建议/提示

【问题讨论】:

    标签: .htaccess rewrite subdomain


    【解决方案1】:

    为了使您的重写规则易于管理,您最好在不引入新变量的情况下转换 url。

    在你的例子中,不可能使用简单的文本替换从 somepage.php 到 sub_page.php 然后从 otherpage.php 到 sub_page2.php - 你需要为每个页面设置一个新规则,因为只有你知道关于映射。将结果 url 建立在源 url 中的数据上会容易得多。

    例如,您可以设计一个方案:

    demo.example.com/somepage.php?start=5
    

    变成

    www.example.com/sub_somepage.php?user=demo&start=5
    

    使用此方案,目标脚本的名称可以从原始脚本的名称中导出。此方案的 RewriteRule 如下所示:

    RewriteRule ^/([A-Za-z0-9_]+\.php) /sub_$1?user=%1 [qsappend]
    

    让重写规则正常工作可能非常棘手。在您完善规则时,您可能希望将其添加到您的配置中:

    RewriteLog "/tmp/httpd-rewrite.log"
    RewriteLogLevel 5
    

    然后,您可以检查您的规则是否在执行您想要的操作。有关这些指令如何工作的更多信息,请访问 httpd.apache.org。特别是,如果您想重定向到不同的主机以提供内容,则必须将新主机名添加到替换和 [redirect] 标志中。

    【讨论】:

    • RewriteRule ^/([A-Za-z0-9_]+\.php) /sub_$1?user=%1 [qsappend] 这适用于 sub_index.php 但在 sub_other_page 中出现 404 错误.php 两个页面都上传到根目录
    猜你喜欢
    • 2016-11-22
    • 1970-01-01
    • 2012-03-22
    • 2021-08-08
    • 1970-01-01
    • 2014-05-20
    • 2019-07-29
    • 2023-01-18
    • 2016-04-20
    相关资源
    最近更新 更多