Nginx自動跳轉到帶www域名規則配置、Nginx多域名向主域名跳轉

nginx 域名跳轉 Nginx跳轉自動到www域名規則配置,若是設置使 xxx.com 域名在用戶訪問的時候自動跳轉到 www.xxx.com 呢?nginx

1、在你的域名管理裏面定義 xxx.com 和 www.xxx.com 指向你的主機ip地址,可使用 nslookup 命令測試學習

直接輸入 nslookup xxx.com 和 nslookup www.xxx.com 都有指向ip的A記錄便可。測試

 

2、在nginx裏面配置 rewrite 規則。打開 Nginx.conf 文件找到server配置段:【如下是個人server配置段】搜索引擎

#############禁止IP地址訪問###########
server{
  listen 80 default_server;
  server_name _;
  return 403;
}
#############禁止IP地址訪問###########

server {
  listen 80;
  server_name www.xxx.com xxx.com;
  if ($host != 'www.xxx.com') {
  rewrite ^/(.*)$ http://www.xxx.com/$1 permanent;
  }
}

這樣就是用戶直接訪問 xxx.com 直接跳轉的www.xxx.com。即讓不帶 www 的域名跳轉到帶 www 的域名。spa

 

3、拓展.net

能夠是多個二級域名、三級域名均可以隨意跳轉、或者讓它們都跳轉到 xxx.xxx.com 這個域名,添加以下語句便可code

server {
  listen 80;
  server_name xxx.xxx.com xxx.xxx.org;
  if ($host != 'xxx.xxx.org') {
  rewrite ^/(.*)$ http://xxx.xxx.com/$1 permanent;
  }
}

上面這個可讓另一個二級域名 xxx.xxx.org 跳轉到 xxx.xxx.comserver

這樣不至於放棄二級域名後,對搜索引擎形成影響。對seo頗有幫助blog

更多nginx規則,歡迎你們一塊兒學習!相互提升!索引

相關文章
相關標籤/搜索