如何使用 Apache Rewrite 将用户重定向到完全限定的域名?

发布于 2024-09-05 18:28:04 字数 240 浏览 1 评论 0原文

我对 apache mod_rewrite 模块真的很陌生。我的公司内网中有一个名为 http://abc 的页面。我希望用户每次在 URL 栏中输入 http://abc 时都会被重定向到 http://abc.somecompanyname.com。有人可以提供示例或指出我正确的方向吗?

我认为这应该是一个很容易回答的问题。感谢大家的投入。

-标记

I'm really new to apache mod_rewrite module. I have a page called http://abc in my company intranet. I want users to be redirected to http://abc.somecompanyname.com whenever they type http://abc to the URL bar. Could someone please provide and example or point me in the right direction.

I figure this should be quite an easy question to answer. Thanks everyone for you inputs.

-Mark

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

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

发布评论

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

评论(3

二手情话 2024-09-12 18:28:04

引自 Apache 2.4 文档

解决这个问题的最佳方法根本不涉及 mod_rewrite,而是使用放置在虚拟主机中的重定向指令作为非规范主机名。

<VirtualHost *:80>
  ServerName undesired.example.com
  ServerAlias example.com notthis.example.com

  Redirect / http://www.example.com/
</VirtualHost>

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

这确实需要另一个虚拟主机,但并不缺少这些。该解决方案对我来说非常有效 - 我喜欢如何将“不需要的”主机的重定向和规范主机的配置分开。

Quote from Apache 2.4 documentation:

The very best way to solve this doesn't involve mod_rewrite at all, but rather uses the Redirect directive placed in a virtual host for the non-canonical hostname(s).

<VirtualHost *:80>
  ServerName undesired.example.com
  ServerAlias example.com notthis.example.com

  Redirect / http://www.example.com/
</VirtualHost>

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

This does require another virtual host, but there's no shortage of those. The solution works very well for me - and I like how redirection of 'unwanted' hosts and configuration of the canonical host are separated.

许你一世情深 2024-09-12 18:28:04

您可以使用像这样简单的 VirtualHost 定义来完成此任务,在处理 abc 请求的服务器上:

<VirtualHost *:80>
    ServerName abc
    RewriteEngine on
    RewriteRule ^/(.*)$ http://abc.somecompanyname.com/$1 [R,L]
</VirtualHost>

You could accomplish that with a VirtualHost definition as simple as this, on the server handling requests for abc:

<VirtualHost *:80>
    ServerName abc
    RewriteEngine on
    RewriteRule ^/(.*)$ http://abc.somecompanyname.com/$1 [R,L]
</VirtualHost>
回心转意 2024-09-12 18:28:04

我发现 Apache2 URL 重写指南 中的建议效果更好。

我最终得到:

RewriteEngine on
RewriteCond %{HTTP_HOST}   !^foo\.bar\.com [NC]
RewriteCond %{HTTP_HOST}   !^$ 
RewriteRule ^/(.*)         http://foo.bar.com/$1 [L,R]

Apache2 示例中未包含“RewriteEngine on”行。也许它通常是默认打开的,但就我而言,我需要添加它。

I found the advise in the Apache2 URL Rewriting Guide worked better.

I ended up with:

RewriteEngine on
RewriteCond %{HTTP_HOST}   !^foo\.bar\.com [NC]
RewriteCond %{HTTP_HOST}   !^$ 
RewriteRule ^/(.*)         http://foo.bar.com/$1 [L,R]

The "RewriteEngine on" line wasn't included in the Apache2 example. Maybe it's usually on by default but in my case I needed to add it.

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