对子域使用 mod_rewrite
我不希望人们能够在我的网站中注册他们自己的部分,并将这些部分作为我网站的他们自己的子域。 所以有人可以注册“test”并让“test.example.com”引用他们的网站,该网站位于 /site.php?id=1 那么我将如何为此编写 mod_rewrite 规则呢?
I wan't people to be able to register their own sections within my site, and have those sections be their own subdomain of my site. so someone can register 'test' and have 'test.example.com' refer to their site which would be at say /site.php?id=1 So how would i go about writing a mod_rewrite rule for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
mod_rewrite
可能不是最好的工具。 RewriteRules 非常适合将文件映射到其他内容,但它并不是真正用于域匹配。使用
mod_rewrite
可以做的最好的事情如下:这并不是真正有用。 如果以编程方式检查
HTTP_HOST
的值,我建议您这样做:mod_rewrite
might not be the best tool for this. RewriteRules are great for mapping a file to something else, but it was not really meant for domain matching.The best you can do using
mod_rewrite
is the following:Which is not really useful. What I would recommend to do instead if programmatically check the value of
HTTP_HOST
as such:您可以将 apache 配置为在您的虚拟主机容器中声明 apache 配置中的“特殊”域(例如 www.mydomain.com 等),并将其他域默认为您的第一个虚拟主机。 然后,您可以将基本 URL 重写到调度应用程序,该应用程序检查 URL 并将用户发送到正确的区域。
来自 Apache 2.0 文档:
将处理 *.mydomain.com 的代码放入第一个虚拟主机中:
在dispatch.cgi 中,检查调用 URL 以确定用户应该去哪里。 确保您将 DNS 设置为接受所有子域。
希望有帮助!
You can configure apache to claim the "special" domains in the apache configuration (e.g. www.mydomain.com, etc), in your virtual host containers and have the others default to your first virtual host. You can then rewrite the base url to a dispatch application that inspects the URL and sends the user off to their correct area.
From Apache 2.0 docs:
Put the code to handle *.mydomain.com in the first virtual host:
In dispatch.cgi, inspect the calling url to determine where the user should go. Make sure you set up your DNS to accept all subdomains.
Hope that helps!
事实上,过去一年我们一直在使用这种技术,没有出现太大问题。 我们的 mod_rewrite 配置如下所示:
发生的情况是,用户被定向到theiraccount.mydomain.com/admin。
然后我们使用 PHP 来识别域名的帐户部分。 使用 $_SERVER['SERVER_NAME'] 之类的东西或某种获取域名并解析它的方法效果很好。
不过,请将这些配置保留在 httpd.conf 中,如果保留为 .htaccess,这将在高流量站点中成为 CPU 问题。
Actually, we've been using this technique without much problem for the past year. Our mod_rewrite config looks like:
What happens, is that users are directed to theiraccount.mydomain.com/admin.
We then use PHP to identify the account portion of the domain name. Using something like $_SERVER['SERVER_NAME'] or some method of getting the domain name and parsing it works very well.
Keep these configs in the httpd.conf though, this will become a cpu problem in higher traffic sites if it's kept as .htaccess.