【问题标题】:how to show the content of a wildcard subdomain?如何显示通配符子域的内容?
【发布时间】:2018-12-23 19:16:49
【问题描述】:

当用户进入子域(我将有通配符子域)时,我该怎么做,他会看到主域中具有相同名称的子文件夹中的内容?例如,如果用户将进入works.domain.com,我希望他查看www.domain.com/works 中的内容。

这是我的方法:

  1. 我创建了一个 通配符 子域,例如 *.domain.com
  2. 使用 index.php 文件在通配符目录中创建了一些子域,因为我可以检查有关此商店的其他信息的首选项 -> 请参阅:

  1. 现在我的目的是向用户提供他们在 url 中要求的内容

这段代码写在wildcard/index.php文件中

  <?php 

 //Get the store name from url
 $host = $_SERVER['HTTP_HOST'];
 $url = explode('.', $host)[0];

 //Find the store name is it available in the database
 $db      = new database;
 $qri     = $db->query("SELECT * FROM store_info WHERE store_name='$url'");
 $count   = mysqli_num_rows($qri);

 //If it returns true then show the reuquested store data
 if($count != 0){
   *I want to show here the folder data that requested in the url*
 }else{
   echo 'Store Not found';
 }

现在我的问题是:

  1. 这样做的方法是否正确,那么如何显示请求的文件夹数据?

【问题讨论】:

  • 这里对SQL注入开放,你知道吗?
  • 是的,我知道我只是在这里展示了一个示例

标签: php .htaccess wildcard-subdomain


【解决方案1】:

您可以在 PHP 服务器前配置 NGINX,以使用以下配置(未测试)路由到特定目录。在 NGINX 中配置它比在 PHP 中配置它更有效。

server {
      server_name ~^(?<sub>[a-zA-Z0-9-]+)\.domain\.com$; # will cause the sub-domain to be placed in $sub
      return 301 "http://testsite.net/${sub}${uri}";
    }

来源:https://serverfault.com/questions/426673/nginx-redirect-subdomain-to-sub-directory

【讨论】:

  • 但是我需要检查更多的东西,比如订阅类型/账单等,因为我可以确定他们是否有资格使用商店。我的意思是,如果他们不支付账单,那么我可以在商店删除或过期时向他们展示
  • 你可以在你的 PHP 代码中处理你想要做的事情。该 URI 参数应将您的原始 URI 数据转发到新的重定向 URL。
  • 我应该把它写在一个。 htaccess 文件你提供的代码?
猜你喜欢
  • 1970-01-01
  • 2019-05-12
  • 2012-11-24
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 2020-08-23
  • 1970-01-01
  • 2017-08-23
相关资源
最近更新 更多