PHP:使用htaccess为每个用户动态子域

发布于 2024-09-17 00:04:18 字数 261 浏览 4 评论 0原文

是否可以使用 htaccess 为每个用户创建动态子域。例如,如果有一个用户的用户名为 myusername,则为他添加一个子域,例如 htttp://www.myusername.example.com,当有人加载此页面时,它应该为 http://www.example.com/?user=myusername (使用 htaccess )

is it possible to create dynamic sub domains for each user using htaccess. for example if there is a user with user name myusername, then add a sub domain for him like htttp://www.myusername.example.com, and when somebody load this page it should come as http://www.example.com/?user=myusername ( using htaccess )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

桃酥萝莉 2024-09-24 00:04:18

您必须首先调整您的服务器软件。
?user=myusername 是主机 www.example.com 上的路径
而 www.myusername.example.com 是另一个主机
浏览器必须知道 www.myusername.example.com 域应该调用哪个 IP 地址。

因此,首先您必须设置 DNS 记录,使所有子域成为主域的别名。
接下来,您必须设置 Web 服务器软件以接受所有子域。
最后您可以处理任何子域请求。不涉及 .htaccess。

有关详细信息 - 请参阅之前在此回答的许多相同问题。

You have to tune your server software first.
?user=myusername is a path at the host www.example.com
while www.myusername.example.com is another host
A browser have to know which IP address should be called for www.myusername.example.com domain.

Thus, first of all you have to set up a DNS record to make all subdomains an aliases to main domain.
Next, you have to set up your web server software to accept all subdomains.
And finally you can process any subdomain request. no .htaccess involved.

For the details - refer to numerous same questions previously answered here on SO.

未央 2024-09-24 00:04:18

是的,这是可能的!

您可以使用如下内容:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule . http://example\.com/?user=%2/ [L]
</IfModule>

确保 *.example.com 设置为指向 example.com 的服务器。 (您需要为其创建一个名称记录。您必须使用“通配符子域”,如“balupton”所指出)

Yes, It is possible!

You can use something like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule . http://example\.com/?user=%2/ [L]
</IfModule>

Make sure *.example.com is set to point to the server of example.com. (You will need to create a name record for the same. You have to use "Wildcard Subdomain", as pointed by 'balupton')

瞄了个咪的 2024-09-24 00:04:18

我不相信您可以在 .htaccess 文件中执行此操作。您所描述的是配置许多 虚拟主机 由给定的 Apache 实例。虚拟主机必须在 Apache 服务器配置文件中配置,而不是在 .htaccess 文件中。请参阅文档。

I don't believe you can do this in .htaccess files. What you're describing is configuring a number of Virtual Hosts to be served by a given instance of Apache. Virtual hosts must be configured in the Apache server config files, not the .htaccess files. See documentation.

知你几分 2024-09-24 00:04:18

您可以使用 .htaccess 文件部分完成此操作,但某些配置必须在 httpd.conf 级别完成。基本上,它看起来像这样:

a) 将您的域配置为具有该区域的通配符 dns。有关如何执行此操作的确切详细信息取决于您的 DNS 提供商是谁,或者您正在运行哪种 BIND 软件。但基本上进行设置,以便 *.example.com 指向您的服务器地址。
b) 像这样配置网络服务器:

<VirtualHost x.x.x.x:80>
    ServerName *.example.com
    ...
</VirtualHost>

<VirtualHost x.x.x.x:80>
    Server some.fixed.subdomain.example.com
    ...
</VirtualHost>

确保在通配符条目之后列出所有非动态域,否则很可能无法正常工作。 Apache 对设置的顺序相当挑剔。

通过此设置,无需将请求重写为查询。您可以让脚本检查 $_SERVER['HTTP_HOST'] 以找出正在提供服务的虚拟子域并从那里开始工作。

You can do this partially with .htaccess files, but some of the configuration will have to be done at the httpd.conf level. Basically, it'd looked like this:

a) Configure your domain to have wildcard dns for the zone. Exact details on how to do this depends on who your DNS provider is, or which BIND software you're running. But basically set things up so that *.example.com points at your server's address.
b) Configure the webserver like this:

<VirtualHost x.x.x.x:80>
    ServerName *.example.com
    ...
</VirtualHost>

<VirtualHost x.x.x.x:80>
    Server some.fixed.subdomain.example.com
    ...
</VirtualHost>

Make sure you list any non-dynamic domains AFTER the wildcard entry, or things will most likely not work. Apache's rather picky about the order this gets set up in.

With this setup, there's no need to rewrite requests into a query. You can have your scripts check $_SERVER['HTTP_HOST'] to figure out which virtual sub-domain is being served and work from there.

世界如花海般美丽 2024-09-24 00:04:18

您需要的功能称为通配符子域。它允许您不必为每个子域设置 DNS,而是使用 apache 重写进行重定向。您可以在此处找到一个不错的教程,但教程有数千个在那里。以下是该教程中的必要代码:

<VirtualHost 111.22.33.55>
    DocumentRoot /www/subdomain
    ServerName www.domain.tld
    ServerAlias *.domain.tld
</VirtualHost>

但是,由于它需要使用 VirtualHosts,因此必须在服务器的 httpd.conf 文件中设置,而不是本地 .htaccess。

The feature you are after is called Wildcard Subdomains. It allows you not have to setup DNS for each subdomain, and instead use apache rewrites for the redirection. You can find a nice tutorial here, but there are thousands of tutorials out there. Here is the necessary code from that tutorial:

<VirtualHost 111.22.33.55>
    DocumentRoot /www/subdomain
    ServerName www.domain.tld
    ServerAlias *.domain.tld
</VirtualHost>

However as it required the use of VirtualHosts it must be set in the server's httpd.conf file, instead of a local .htaccess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文