如何實現多個二級域名301重定向跳轉
寶塔Linux面板中,有著自帶的301重定向功能(市面上很多面板都有這個功能),然而這是實現綁定域名(主域)301跳轉的,如果你要將主域下的好幾個二級域名301重定向,就無法直接進行301了,這個時候,就需要使用"配置文件"這個功能了,使用步驟如下:
步驟一
-
選擇主域 > 設置 > 配置文件
步驟二
- 修改代碼,我們主域以“xxx.com”為例,要給二級域名“a.xxx.com”和“b.xxx.com”做301分別跳轉到“網址1.com”和“網址2.com”,即
a.xxx.com 跳到 網址1.com
b.xxx.com 跳到 網址2.com
配置文件里的代碼如下:
server
{
listen 80;
server_name xxx.com www.xxx.com;
index index.html index.php index.htm default.php default.htm default.html;
root /www/wwwroot/xxx;
#SSL-START SSL相關配置,請勿刪除或修改下一行帶注釋的404規則
#error_page 404/404.html;
#301-START
if ($host ~ '^xxx.com'){
return 301 http://www.xxx.com$request_uri;
}
#301-END
#SSL-END
#ERROR-PAGE-START 錯誤頁配置,可以注釋、刪除或修改
error_page 404 /404.html;
error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP引用配置,可以注釋或修改
include enable-php-54.conf;
#PHP-INFO-END
#REWRITE-START URL重寫規則引用,修改后將導致面板設置的偽靜態規則失效
include /www/server/panel/vhost/rewrite/xxx.com.conf;
#REWRITE-END
#禁止訪問的文件或目錄
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
#一鍵申請SSL證書驗證目錄相關設置
location ~ \.well-known{
allow all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log off;
access_log off;
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log off;
access_log off;
}
access_log /www/wwwlogs/xxx.com.log;
error_log /www/wwwlogs/xxx.com.error.log;
}
#BINDING-a.xxx.com-START
server
{
listen 80;
server_name a.xxx.com;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/xxx/a;
#301-START
if ($host ~ '^a.xxx.com'){
return 301 http://www.網址1.com$request_uri;
}
#301-END
include enable-php-54.conf;
include /www/server/panel/vhost/rewrite/xxx.com.conf;
#禁止訪問的文件或目錄
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
#一鍵申請SSL證書驗證目錄相關設置
location ~ \.well-known{
allow all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log off;
access_log off;
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log off;
access_log off;
}
access_log /www/wwwlogs/xxx.com.log;
error_log /www/wwwlogs/xxx.com.error.log;
}
#BINDING-a.xxx.com-END
#BINDING-b.xxx.com-START
server
{
listen 80;
server_name b.xxx.com;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/xxx/b;
#301-START
if ($host ~ '^b.xxx.com'){
return 301 http://www.網址2.com$request_uri;
}
#301-END
include enable-php-54.conf;
include /www/server/panel/vhost/rewrite/xxx.com.conf;
#禁止訪問的文件或目錄
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
#一鍵申請SSL證書驗證目錄相關設置
location ~ \.well-known{
allow all;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log off;
access_log off;
}
location ~ .*\.(js|css)?$
{
expires 12h;
error_log off;
access_log off;
}
access_log /www/wwwlogs/xxx.com.log;
error_log /www/wwwlogs/xxx.com.error.log;
}
#BINDING-b.xxx.com-END
認真觀察,其實不難發現,主要發揮作用的是下面這段代碼,因為我們為主域做301的時候,配置文件就會生成這串代碼,同理,把這串代碼復制到二級域名的配置代碼中,修改參數即可。
#301-START
if ($host ~ '^a.xxx.com'){
return 301 http://www.網址1.com$request_uri;
}
#301-END
保存后,記得清理下本地緩存,再輸入域名,看看有沒有成功實現301重定向。
使用以上方式進行301的前提是你的二級域名用的是【子目錄綁定】功能來實現二級域名,而不是直接將二級域名作為一個主域建立網站的!