如何使用 Apache 创建临时和永久重定向

介绍

HTTP 重定向 是指向一个域或地址到另一个方法. 有几种不同的重定向,每个重定向都意味着客户端浏览器的不同。

Temporary redirects(响应状态代码 302 找到 )是有用的,如果 URL 暂时需要从不同的位置提供服务。

Permanent redirects(响应状态代码 301 永久移动 ),另一方面,告知浏览器应该完全忘记旧地址,不再试图访问它。

在 Apache 中,您可以通过在服务器配置文件中的虚拟主机输入中添加这样的行来创建临时重定向:

1Redirect /oldlocation http://www.newdomain.com/newlocation

同样,使用这样的行为永久重定向:

1Redirect permanent /oldlocation http://www.newdomain.com/newlocation

本指南将更深入地解释如何在Apache中实施每个类型的重定向,并通过一些具体使用案例的示例。

前提条件

要遵循本教程,您将需要:

您可以通过以下方法来设置Ubuntu 16.04上的Apache虚拟主机(https://andsky.com/tech/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-16-04),在CentOS 7(https://andsky.com/tech/tutorials/how-to-set-up-apache-virtual-hosts-on-centos-7),或在Debian 7(https://andsky.com/tech/tutorials/how-to-set-up-apache-virtual-hosts-on-debian-7)。

解决方案在一眼

在Apache中,您可以使用重定向指令实现简单的单页重定向,该指令包含在mod_alias模块中,该模块在新的Apache安装中默认启用。

在最简单的形式中,您可以在服务器配置中使用以下行实现临时重定向:

 1[label Temporary redirect with Redirect]
 2<VirtualHost *:80>
 3    ServerName www.domain1.com
 4    Redirect / http://www.domain2.com
 5</VirtualHost>
 6
 7<VirtualHost *:80>
 8    ServerName www.domain2.com
 9    . . .
10</VirtualHost>

此重定向指示浏览器将所有对www.domain1.com的请求定向到www.domain2.com

要重定向不止一页,您可以使用)来指定整个目录,而不只是单个文件. 重定向匹配与括号中的正则表达式模式相匹配,然后使用1'表达式在重定向目的地引用匹配文本,其中1'是首组匹配文本。 在更复杂的例子中,后续相匹配的组按顺序给出数字.

例如,如果您想暂时将www.domain1.com中的每个页面重定向到www.domain2.com,您可以使用以下方法:

 1[label Temporary redirect with RedirectMatch]
 2<VirtualHost *:80>
 3    ServerName www.domain1.com
 4    RedirectMatch ^/(.*)$ http://www.domain2.com/$1
 5</VirtualHost>
 6
 7<VirtualHost *:80>
 8    ServerName www.domain2.com
 9    . . .
10</VirtualHost>

默认情况下,两个重定向重定向Match指令都设置了一个临时重定向,如果你想创建一个永久重定向,你可以通过将永久添加到任何一个指令:

1[label Permanent redirects]
2Redirect permanent / http://www.domain2.com
3RedirectMatch permanent ^/(.*)$ http://www.domain2.com/$1

您还可以使用)。

让我们再来谈谈一些具体的例子。

示例 1 - 迁移到不同的域名

如果您已经建立了网络存在,并希望将您的域名更改为新地址,最好不要只是放弃您的旧域名. 对您的网站的标记和位于整个互联网的其他页面上的链接将被打破,如果您的内容消失,而没有向浏览器指示如何找到新的位置。

在这个例子中,我们将配置一个重定向从旧的域名称为domain1.com到新的域名称为domain2.com

假设您的网站已配置为由一个域名名为domain1.com的域名,已在Apache中配置如下:

1[label /etc/apache2/sites-available/domain1.com.conf]
2<VirtualHost *:80>
3    ServerAdmin [email protected]
4    ServerName domain1.com
5    ServerAlias www.domain1.com
6    DocumentRoot /var/www/domain1.com/public_html
7    ErrorLog ${APACHE_LOG_DIR}/error.log
8    CustomLog ${APACHE_LOG_DIR}/access.log combined
9</VirtualHost>

我们还将假设您已经在domain2.com服务您的未来的网站版本:

1[label /etc/apache2/sites-available/domain2.com.conf]
2<VirtualHost *:80>
3    ServerAdmin [email protected]
4    ServerName domain2.com
5    ServerAlias www.domain2.com
6    DocumentRoot /var/www/domain2.com/public_html
7    ErrorLog ${APACHE_LOG_DIR}/error.log
8    CustomLog ${APACHE_LOG_DIR}/access.log combined
9</VirtualHost>

让我们更改domain1.com虚拟主机配置文件以添加永久重定向到domain2.com:

 1[label /etc/apache2/sites-available/domain1.com.conf]
 2<VirtualHost *:80>
 3    ServerAdmin [email protected]
 4    ServerName domain1.com
 5    ServerAlias www.domain1.com    
 6    DocumentRoot /var/www/domain1.com/public_html
 7    ErrorLog ${APACHE_LOG_DIR}/error.log
 8    CustomLog ${APACHE_LOG_DIR}/access.log combined
 9    RedirectMatch permanent ^/(.*)$ http://domain2.com/$1
10</VirtualHost>

我们使用)$常规表达式对应于URL中的/之后的一切。例如,http://domain1.com/index.html`将被重定向到http://domain2.com/index.html。为了实现永久性重定向,我们只需在RedirectMatch指令之后添加永久性

<$>[注] 注: 请记住在使用systemctl restart apache2进行配置更改后重新启动Apache。

示例 2 — 尽管单页名称更改,但创建持续的体验

有时,需要更改已经发布并在您的网站上收到流量的单个页面的名称。单独更改名称会导致访问者试图访问原始 URL 的 404 Not Found 错误,但您可以通过使用重定向来避免这一点。

假设您的网站有两个单独的产品和服务页面,分别称为products.htmlservices.html

我们假定您的网站配置如下:

1[label Assumed original virtual host configuration]
2<VirtualHost *:80>
3    ServerName example.com
4    . . .
5</VirtualHost>

配置重定向就像使用两个重定向指令一样简单。

1[label Redirects added to the original configuration]
2<VirtualHost *:80>
3    ServerName example.com
4
5    Redirect permanent /products.html /offer.html
6    Redirect permanent /services.html /offer.html
7    . . .
8</VirtualHost>

重定向指令接受必须重定向的原始地址以及新页面的目的地地址. 由于此处的更改不是暂时的,我们在指令中也使用了永久性

结论

请确保使用正确的重定向类型,因为不当使用临时重定向可能会损害您的搜索排名。

HTTP 重定向有许多其他用途,包括强制安全的 SSL 连接(即使用)并确保所有访问者只能在网站的`www。

如果您想了解更多有关您可以重定向访客的方式,Apache 在官方文档的部分(http://httpd.apache.org/docs/current/mod/mod_alias.html)和(http://http://httpd.apache.org/docs/current/mod_rewrite.html)有很好的文档。

Published At
Categories with 技术
comments powered by Disqus