apache - 将所有 URL 重定向到 httpd.conf 中的单个页面

标签 apache redirect httpd.conf

我正在寻找一种将整个网站重定向到一个域的方法。我想在 httpd.conf 文件中执行此操作。例如,我的 VirtualHost 看起来像:

<VirtualHost xx.xx.xx.xx>
ServerName website.com
ServerAdmin <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1c6b797e717d6f68796e5c6b797e6f756879327f7371" rel="noreferrer noopener nofollow">[email protected]</a>
ScriptAlias /cgi-bin/ /usr/local/apache2/htdocs/cgi-bin/
DocumentRoot /usr/local/apache2/htdocs/website
CustomLog logs/website/access_log combined
ErrorLog logs/website/error_log
Redirect / http://newwebsite.com
Redirect /xxx http://newwebsite.com
</VirtualHost>

目前上面的重定向仍然保留完整的 URL。

所以如果我想 http://website.com/test/thispage.html我会重定向到http://newwebsite.com/test/thispage.html 。我只想让它转到 newwebsite.com 并丢失其余的。

所以..

http://website.com/test/thispage.html

http://newwebsite.com

我需要在 httpd.conf 中添加什么才能实现此目的?

最佳答案

正如您所注意到的,Redirect 会捕获所有请求并将相同的 URI 发送到您不想要的目标,因此您将使用 RedirectMatch 来匹配所有可能的请求,但不会向目标 url 添加任何内容。

所以你可以使用:

RedirectMatch ^ http://newsite.example.com/

作为关于 mod_rewrite 的附注: 我当然不会像其他人推荐的那样推荐 mod_rewrite ,除非您手头确实有一项任务需要只有 mod_rewrite 才能提供的复杂性,但事实并非如此。

旁注2: 例如,最好使用“example.com”域名,因为它不是有效的域,并且会永久保留用于示例目的。

关于apache - 将所有 URL 重定向到 httpd.conf 中的单个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38403165/

相关文章:

asp.net - 在重定向中使用 Asp.Net MVC TempData 时是否可能出现 "race condition"?

php - php5 和 httpd 的奇怪问题

windows - Apache 2 - 在 Windows 上重新加载配置

php - 在 php 中更改目录权限 (chmod)

apache - 仅将非 www 重定向到 www - 忽略所有子域

.htaccess:此处不允许使用 RewriteEngine

重定向和谷歌问题

.htaccess 重定向重写规则

mod-rewrite - 添加代理传递后,apache 重写规则不起作用

php - 如何在 Apache 2.2 上使用 PHP 5.4 从 PUT 请求中获取数据