Apache - 子域、mod_rewrite 和 virtualdocumentroot

发布于 2024-09-29 17:13:38 字数 942 浏览 6 评论 0原文

任何人都可以建议我如何实现以下目标(我无法使用 mod_rewrite 或 virtualdocumentroot 编写解决方案)::

1)允许我的网站上的任何子域,(*.mysite.com 不应给出 404 错误)并且

2)确保用户在浏览器中看到的 URL 就是他输入的 URL。

3)无重定向

(因此,如果用户输入 avalidsubdomain.mywebsite.com,则应从相应的目录提供页面。此时我不关心需要保留的目录结构。只要它有效,一切都可以。另一方面,如果他输入 somethatdoesnotexist.mywebsite.com,他不应该得到 404,而应该被带到网站上的自定义页面)。

例如,查看 blogspot.com 上的任何子域都可以正常工作 - 例如 http://dsadsadsdsadsd.blogspot.com/。 (当然谷歌就是谷歌,他们可以编写自己的网络服务器)。但我确信像这样简单的事情可以在 Apache 中完成。

笔记: 1)我不想每次添加有效的子域时都添加/更改apache配置或.htaccess。

2)根据我在各个线程上读到的内容,我可以使用 mod_rewrite 来实现包罗万象的子域规则,但是用户在浏览器中看到的 URL 不会保持不变。

谢谢!

干杯,

JP


我问了一个相关的问题,那里的答案似乎解决了问题。 在此提及,以防有人看到此问题:

mod_rewrite regex(重定向过多)

Can anyone suggest how can I achieve the following (I couldn't cook a solution using either mod_rewrite or virtualdocumentroot)::

1) Allow any subdomain on my website, (*.mysite.com should not give 404 error) AND

2) Ensure that the URL that the user sees in the browser is what he typed.

3) No redirects

(Thus, if user types avalidsubdomain.mywebsite.com, pages should be served from corresponding directory. At this moment I don't care about the directory structure I need to keep. Anything is fine as long as it works. On the other hand, if he types somethingthatdoesnotexist.mywebsite.com, he should not get a 404, but instead, should be taken to a custom page on the website).

For example, see that any subdomain on blogspot.com will work - like http://dsadsadsdsadsd.blogspot.com/. (Of course Google is Google and heck they can write their own web server). But I am sure something as simple as this can be done in Apache.

Notes:
1) I don't want to add to/alter the apache config or .htaccess every time I add a valid subdomain.

2) From what I have read on various threads, I can use mod_rewrite for a catchall subdomain rule, but then, the URL the user sees in the broser does not remain the same.

thanks!

cheers,

JP


I have asked a related question and the answers there seem to solve the problem.
Mentioning here in case someone sees this question:

mod_rewrite regex (too many redirects)

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

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

发布评论

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

评论(4

无人接听 2024-10-06 17:13:38

您想要设置的场景可以使用 Apache httpd 的 mod_vhost_alias 来实现。

您只需要创建一个默认虚拟主机来捕获不存在的子域,请参阅An虚拟主机匹配深入讨论了解详情。

例子:

<VirtualHost _default_:80>
  ServerName example.com
  DocumentRoot /srv/www/custom_page
  # ...
</VirtualHost>

UseCanonicalName Off
VirtualDocumentRoot /srv/www/%1/%2+

The scenario you want to set up can be implemented with mod_vhost_alias for Apache httpd.

You just need to create a default virtual host to catch the non-existing subdomains, see An In-Depth Discussion of Virtual Host Matching for details.

Example:

<VirtualHost _default_:80>
  ServerName example.com
  DocumentRoot /srv/www/custom_page
  # ...
</VirtualHost>

UseCanonicalName Off
VirtualDocumentRoot /srv/www/%1/%2+
笑,眼淚并存 2024-10-06 17:13:38

您所描述的内容可以通过 DNS 完成,并且您必须使用 通配符 来提取关掉它。

What you're describing can be done with DNS, and you'll have to use wildcards to pull it off.

金橙橙 2024-10-06 17:13:38

其价值在于:

我提出了 一种使用 mod_rewrite 以及 VirtualDocumentRoot /.../% 的方法0 将不存在的子域重定向到相应的根域。这确实会更新客户端 URL,但您可以使用 [P] RewriteRule 标志来隐藏它。如您的示例所示,重定向到占位符页面也很简单。

设置完成后,您无需编辑任何配置设置即可添加或删除域或子域。

For what its worth:

I have come up an approach that uses mod_rewrite along with VirtualDocumentRoot /.../%0 to redirect non-existent sub-domains to corresponding root domains. This does update the client URL but you could use a [P] RewriteRule flag to hide this. It would also be trivial to redirect to a placeholder page as in your example.

Once setup, you do not have to edit any configuration settings to add or remove domains or sub-domains.

薄荷港 2024-10-06 17:13:38

在您的 httpd.conf 中添加

<VirtualHost *:80>
        ServerName default
        ServerAlias *
        VirtualDocumentRoot /htdocs/%-2.0.%-1/%-3
</VirtualHost>

Wich 会将您的域名 www.example.com 指向
/htdocs/example.com/www/

然后在您的 rooit 文件夹中添加一个 .htaccess 文件:
/htdocs/example.com/.htaccess 其中包括:
现在,每个不存在的域都将被重定向到 www,状态代码为 301(永久移动)。您可以将 WWW 更改为您想要的任何内容。

# Enable Rewrite Engine
RewriteEngine on

# Rewrite all non-existing sites to www.
RewriteCond %{HTTP_HOST} example\.com$
RewriteRule ^.*$ http://www.example.com%{REQUEST_URI} [R=301,L]

我希望这会对您有所帮助。

in your httpd.conf add

<VirtualHost *:80>
        ServerName default
        ServerAlias *
        VirtualDocumentRoot /htdocs/%-2.0.%-1/%-3
</VirtualHost>

Wich wil point your domain at www.example.com to
/htdocs/example.com/www/

Then add a .htaccess file in your rooit folder:
/htdocs/example.com/.htaccess wich includes:
Now every non-existing domain will be redirected to www with a status code of 301 (Permantly Moved). You can change WWW to whatever you want.

# Enable Rewrite Engine
RewriteEngine on

# Rewrite all non-existing sites to www.
RewriteCond %{HTTP_HOST} example\.com$
RewriteRule ^.*$ http://www.example.com%{REQUEST_URI} [R=301,L]

I hope this will help you.

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