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

时间:2022-12-16 02:09:03

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!

谢谢!

2 个解决方案

#1


15  

You can't make dynamic 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.com,busted.example.com,llama.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.

从那里,解析域名为迈克。

#2


3  

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=$1&title=$2  [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 

#1


15  

You can't make dynamic 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.com,busted.example.com,llama.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.

从那里,解析域名为迈克。

#2


3  

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=$1&title=$2  [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