如何将多个子域用于多个引用


how to use multiple subdomain for multiple cites

我正在一个网站www(dot)shopingmart(dot)com上工作,基本上是一个分类网站,我需要解决我现在面临的一个问题,我花了很多时间但无法解决。 我试图实现。

如果用户访问 newyork.example.com 或 ohio.example.com

这两个城市都指向同一个网站意味着 www.example.com

但显示与城市相关的内容(如 OLX 完成)

我创建了通配符域 *.example.com 与 A 记录我还创建了两个子域

newyork.example.com and 
ohio.example.com 

还要创建 .htaccess 以将流量重定向到索引.php

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

然后解析

$_SERVER['HTTP_HOST'] to obtain subdomain 

但是当我输入 newyork.example.com 它会给我 500 内部错误

请帮助并分享一些想法,我无法解决这个问题

此致敬意

应使用通配符创建虚拟主机,以接受来自特定域的所有子域。

这是我制作的系统的配置示例,基本上可以满足您的需求:

NameVirtualHost *:80
<VirtualHost *:80>
     DocumentRoot /www/your_site
     ServerName example.com
     ServerAlias *.example.com
</VirtualHost>

这使得Apache接收特定域的所有子域(别名)。

您应该首先尝试这种方法,而不使用重写规则,并且仅在通配符起作用后插入规则。

编辑:

关于子域下的资产路径更改,重写规则需要指向文档根目录下的索引.php:

RewriteRule . %{DOCUMENT_ROOT}/index.php [L]