通配符子域名通过.htaccess

时间:2014-07-16 01:18:36

标签: php .htaccess wildcard subdomain

以下是我所拥有的:

每个州对应的数字:-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等等。

任何想法如何?

1 个答案:

答案 0 :(得分: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 /名称关系。这也为您提供了相当容易地以编程方式更新此文件的选项。此示例使用([^\.]*)\.在第一个.之前匹配主机名中的任何内容,然后使用它来查找带有%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
相关问题