如何使用 PHP 和 htaccess 创建动态子域?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17706904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:17:58  来源:igfitidea点击:

How to create dynamic subdomain using PHP and htaccess?

php.htaccess

提问by Ivijan Stefan Stipi?

I have one problem: I want to set up PHP and htaccess to make dynamic subdomains. And I can not figure out how. Currently my URL looks like this:

我有一个问题:我想设置 PHP 和 htaccess 来制作动态子域。我不知道如何。目前我的网址如下所示:

www.exemple.com/index.php?subdomain=mike&component=content&id=26&title=people-love-apps 

I need it to look like this:

我需要它看起来像这样:

www.mike.exemple.com/content/26/people-love-apps.html

I know it can be done but I do not know a solution how to do it. Is very important to me with $ _GET [] functions to read parameter in the URL.

我知道可以做到,但我不知道如何解决。用 $_GET[] 函数读取 URL 中的参数对我来说非常重要。

The problem is that users on my site make their web site and get a free domain (sub-domain) automatically. This should be automatic.

问题是我网站上的用户创建他们的网站并自动获得免费域(子域)。这应该是自动的。

Thanks!

谢谢!

回答by user20232359723568423357842364

You can't makedynamic subdomains with .htaccess

你不能动态子域.htaccess

You will need to configure the apache virtual host to accept requests for multiple domains

您将需要配置 apache 虚拟主机以接受对多个域的请求

See the Apache documentation - Using Name Based Virtual Hosts

请参阅Apache 文档 - 使用基于名称的虚拟主机

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

Adding the wildcard subdomain *.example.com, your PHP application will receive all requests for any domain below example.com, ie garbage.example.com, busted.example.com, llama.example.com, etc.

添加通配符子域名*.example.com,你的PHP应用程序将获得以下任意站点的所有请求example.com,即garbage.example.combusted.example.comllama.example.com等等。

Creating Wildcard Sub Domain Using Apache VirtualHost

使用 Apache VirtualHost 创建通配符子域

Multiple Domains to One Virtual Host | Wildcard Host (shared hosting)?

多个域到一个虚拟主机 | 通配符主机(共享主机)?

Apache default virtual host for multiple domains

多个域的Apache默认虚拟主机

At this point, your application will have to determine the validity of the subdomain and display the appropriate error for unknown subs.

此时,您的应用程序必须确定子域的有效性并为未知子域显示相应的错误。

From there, parse the domain for mike.

从那里,解析域的mike.

回答by tormuto

OF COURSE you can interprete dynamic subdomains with htaccess. The following rule will solve the problem.

当然,您可以使用 htaccess 来解释动态子域。下面的规则将解决这个问题。

RewriteCond %{HTTP_HOST} www\.([^.]+)\.example\.com [NC]
RewriteRule ^/content/([0-9]+)/([a-zA-Z0-9_\-])\.html /index.php?subdomain=%1&component=content&id=&title=  [L]

so anyone visting

所以任何人访问

 www.mike.exemple.com/content/26/people-love-apps.html

will see the content of

会看到内容

www.exemple.com/index.php?subdomain=mike&component=content&id=26&title=people-love-apps