对子域使用 mod_rewrite

发布于 2024-07-29 10:33:24 字数 143 浏览 5 评论 0原文

我不希望人们能够在我的网站中注册他们自己的部分,并将这些部分作为我网站的他们自己的子域。 所以有人可以注册“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 技术交流群。

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

发布评论

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

评论(3

放肆 2024-08-05 10:33:25

mod_rewrite 可能不是最好的工具。 RewriteRules 非常适合将文件映射到其他内容,但它并不是真正用于域匹配。

使用 mod_rewrite 可以做的最好的事情如下:

RewriteEngine On
# Skip www.domain.com
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^/(.*)$  site.php?domain=%1&file=$1 [L]

这并不是真正有用。 如果以编程方式检查 HTTP_HOST 的值,我建议您这样做:

function get_subdomain() {
  if(preg_match('/^([^.]+)\.domain\.com/i', $_SERVER['HTTP_HOST'], $matches)) 
    return $matches[1];
}

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:

RewriteEngine On
# Skip www.domain.com
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
RewriteRule ^/(.*)$  site.php?domain=%1&file=$1 [L]

Which is not really useful. What I would recommend to do instead if programmatically check the value of HTTP_HOST as such:

function get_subdomain() {
  if(preg_match('/^([^.]+)\.domain\.com/i', $_SERVER['HTTP_HOST'], $matches)) 
    return $matches[1];
}
╰◇生如夏花灿烂 2024-08-05 10:33:25

您可以将 apache 配置为在您的虚拟主机容器中声明 apache 配置中的“特殊”域(例如 www.mydomain.com 等),并将其他域默认为您的第一个虚拟主机。 然后,您可以将基本 URL 重写到调度应用程序,该应用程序检查 URL 并将用户发送到正确的区域。

来自 Apache 2.0 文档:

几乎所有 Apache 指令都可以执行
到 VirtualHost 容器中。 这
第一个 VirtualHost 部分用于
没有已知服务器名称的请求。

将处理 *.mydomain.com 的代码放入第一个虚拟主机中:

<VirtualHost *:80>
  Servername custom.mydomain.com
  DocumentRoot /var/www/html/custom
  RewriteEngine on
  RewriteRule ^/$ /var/www/cgi-bin/dispatch.cgi [R]
  # more configs here
</VirtualHost>

<VirtualHost *:80>
  Servername www.mydomain.com
  DocumentRoot /var/www/html/homepage
  # more configs here
</VirtualHost>

在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:

Almost any Apache directive may go
into a VirtualHost container. The
first VirtualHost section is used for
requests without a known server name.

Put the code to handle *.mydomain.com in the first virtual host:

<VirtualHost *:80>
  Servername custom.mydomain.com
  DocumentRoot /var/www/html/custom
  RewriteEngine on
  RewriteRule ^/$ /var/www/cgi-bin/dispatch.cgi [R]
  # more configs here
</VirtualHost>

<VirtualHost *:80>
  Servername www.mydomain.com
  DocumentRoot /var/www/html/homepage
  # more configs here
</VirtualHost>

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!

仙女 2024-08-05 10:33:25

事实上,过去一年我们一直在使用这种技术,没有出现太大问题。 我们的 mod_rewrite 配置如下所示:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.mydomain.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteCond %{REQUEST_URI} !admin
RewriteRule (.*) /admin/ [R=301,L]
RewriteRule ^admin(.*)$ hosted/admin/$1 [QSA,L]

发生的情况是,用户被定向到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:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.mydomain.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com
RewriteCond %{REQUEST_URI} !admin
RewriteRule (.*) /admin/ [R=301,L]
RewriteRule ^admin(.*)$ hosted/admin/$1 [QSA,L]

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.

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