全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 2419|回复: 1

Apache/Nginx 配置 301 重定向

[复制链接]
发表于 2016-5-29 21:03:50 | 显示全部楼层 |阅读模式
Nginx 配置重定向

为了实现 301 重定向,需要在原有的配置文件里多添加一段代码,比如这是我原来的 Nginx 配置文件

  1. server
  2. {
  3.         listen 80;
  4.         listen [::]:80;

  5.         root /var/www/html;

  6.         server_name www.example.com example.com;
  7.         index index.html index.htm index.php;

  8.         location ~ \.php$ {
  9.                 include snippets/fastcgi-php.conf;
  10.                 fastcgi_pass unix:/var/run/php5-fpm.sock;
  11.         }
  12. }
复制代码


我现在要让所有访问带 www 的都重定向到不带 www 的,则需要改成这样

  1. server
  2. {
  3.     listen 80;
  4.     server_name www.example.com;
  5.     return 301 $scheme://example.com$request_uri;
  6. }

  7. server {
  8.         listen 80;

  9.         root /var/www/html;

  10.         server_name www.example.com example.com;
  11.         index index.html index.htm index.php;

  12.         location ~ \.php$ {
  13.                 include snippets/fastcgi-php.conf;
  14.                 fastcgi_pass unix:/var/run/php5-fpm.sock;
  15.         }
  16. }
复制代码


如果要让所有访问不带 www 的都重定向到带 www 也是依葫芦画瓢

  1. server
  2. {
  3.     listen 80;
  4.     server_name example.com;
  5.     return 301 $scheme://www.example.com$request_uri;
  6. }

  7. server
  8. {
  9.         listen 80;

  10.         root /var/www/html;

  11.         server_name www.example.com example.com;
  12.         index index.html index.htm;

  13.         location ~ \.php$ {
  14.                 include snippets/fastcgi-php.conf;
  15.                 fastcgi_pass unix:/var/run/php5-fpm.sock;
  16.         }
  17. }
复制代码


最后不要忘了重新加载一下 Nginx 的配置文件或者重启一下 Nginx 服务

nginx -s reload 或者 service nginx restart

注意:如果是 https 服务器,则将 80 端口改成 443 即可。





Apache 配置重定向


启用 Rewrite 模块

要使用重定向的功能的话首先得先启动 Apache 的 mod_rewrite 模块,然后重启 Apache 服务

  1. a2enmod rewrite
  2. service apache2 restart
复制代码


启用了 Rewrite 模块后就可以愉快的使用 .htaccess 文件来设置 Rewrite 规则了,使用过 WordPress 之类的 CMS 系统的朋友们是不是很眼熟呢?

启用 .htaccess 文件

打开 Apache 的配置文件,在原有配置文件基础上加入

  1. <Directory /var/www/html>
  2.         Options Indexes FollowSymLinks MultiViews
  3.         AllowOverride All
  4.         Order allow,deny
  5.         allow from all
  6. </Directory>
复制代码


其中 /var/www/html 换成你的实际路径,也就是你在配置文件中设置的 DocumentRoot。

保存并重启 Apache 服务~

service apache2 restart


配置 Rewrite 模块

切换到文档根目录,创建一个 .htaccess 文件,然后用 Vim 之类的编辑器打开

  1. cd /var/www/html
  2. touch .htaccess
  3. vim .htaccess
复制代码


如果要让所有访问带 www 的都重定向到不带 www 的,则往 .htaccess 文件中写入

  1. RewriteEngine On
  2. RewriteBase /
  3. RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  4. RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
  5. ```

  6. 如果要让所有访问不带 www 的都重定向到带 www 的,则改成

  7. ``` bash
  8. RewriteEngine On
  9. RewriteBase /
  10. RewriteCond %{HTTP_HOST} !^www\. [NC]
  11. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
复制代码


最后重启一下 Apache 服务就可以了

service apache2 restart


同样的,如果你使用的是 https 服务器,则只需要将上述的 Rewrite 规则中的 http 改成 https 即可。

测试

使用 curl 命令测试一下重定向是否生效


  1. curl -I http://example.com
复制代码


假设我们是把所有不带 www 的都重定向到带 www 了,则返回的结果应该类似于


  1. HTTP/1.1 301 Moved Permanently
  2. Server: nginx/1.6.2
  3. Date: Tue, 16 Jun 2015 09:36:09 GMT
  4. Content-Type: text/html
  5. Content-Length: 193
  6. Connection: keep-alive
  7. Location: http://www.example.com/
复制代码


重点的是 301 Moved Permanently 以及下方的 Location。
发表于 2016-8-14 22:12:32 | 显示全部楼层
很好,学习了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-3-29 14:31 , Processed in 0.094473 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表