【问题标题】:Wildcard subdomains through .htaccess通过 .htaccess 的通配符子域
【发布时间】:2014-09-06 09:16:38
【问题描述】:

这是我所拥有的:

每个州对应数字:-1、-2、-3、-4等,也是$row[countryid]

每个城市对应数字:1、2、3、4等,也是$row[cityid]

(示例:qwikad.com/-1 或 qwikad.com/1)

州和城市的名称与州的 $xcountryname 和城市的 $xcityname 相呼应

我想通过 htaccess 将所有这些链接转换为对用户更友好的子域。

所以,qwikad.com/-1 应该变成 alabama.qwikad.com 并且 qwikad.com/1 应该变成 auburn.qwikad.com 等等等等等等。

有什么想法吗?

【问题讨论】:

    标签: php .htaccess wildcard subdomain


    【解决方案1】:

    您需要将所有子域指向服务器,然后您可以使用RewriteCond 匹配%{HTTP_HOST} 来确定您是否应该进行重写。如果您的子域数量有限,您可以手动执行此操作,如下所示。

    RewriteEngine On
    RewriteBase /
    # Does it start with "alabama", if so do the rewrite on the next line
    RewriteCond %{HTTP_HOST} ^alabama
    RewriteRule (.*) /-1/$1 [L]
    # Does it start with "auburn", if so do the rewrite on the next line
    RewriteCond %{HTTP_HOST} ^auburn
    RewriteRule (.*) /1/$1 [L]
    # ... and so on
    

    对于大量的重定向,您应该使用RewriteMap 来保存用于重写的 ID/Name 关系。这也为您提供了以编程方式相当容易地更新此文件的选项。此示例使用 ([^\.]*)\. 匹配主机名中第一个 . 之前的任何内容,然后使用它从带有 %1 反向引用的文本字段中查找 ID。

    RewriteEngine On
    RewriteBase /
    RewriteMap subdomains txt:/path/to/file/map.txt
    RewriteCond %{HTTP_HOST} ^([^\.]*)\.
    RewriteRule (.*) /${subdomains:%1}/$1 [L]
    

    /path/to/file/map.txt

    #
    # subdomain mappings
    # comments start with a "#"
    # key value pairs one per line
    #
    alabama   -1   # state
    auburn     1   # city
    something  2
    else       3
    

    【讨论】:

    • 刚刚尝试了你的建议,它给了我一个 500 错误消息。
    • 我禁用了错误日志...所以我不知道。但我在网上看到一些托管服务提供商不允许 RewriteMap。真的吗?也许这就是它不起作用的原因。
    • 也许,如果没有实际的错误信息很难说。
    • 好的,我重新打开错误日志并再次测试。基本上它说.htaccess中不允许“RewriteMap”。有没有其他方法可以绕过 .htaccess 或不使用 RewriteMap 命令?
    • 第一个例子没有使用RewriteMap
    猜你喜欢
    • 2011-08-16
    • 1970-01-01
    • 2012-01-23
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-14
    • 2014-04-01
    相关资源
    最近更新 更多