Nginx 301 重定向 redirect 域名配置

301 重定向 redirect

Unsplash+

以搜尋引擎來看網址,有帶 www 和不帶 www 的網址是不同的兩個 URL,當它們指向同一個網站時,會讓搜尋引擎不知應該選擇哪一個 URL 作為主要的網址。

若 WordPress 網站可以進入管理後台,在 WordPress Address (URL) 和 Site Address (URL) 打入網址,決定要不要帶 www 網址。

內容目錄:

  • Nginx 301 重定向
  • HTTP 301 redirect HTTPS
  • 301 重定向新網址
  • Nginx
  • .htaccess on Apache
  • Kinsta 控制台
  • Apache 系統

我剛好代管一個 Joomla 網站,後台就沒有設置的選項,就需要從主機採用 301 重定向 (redirect) 進行 URL 標準化設置。

Nginx 301 重定向

先在 DNS 設置好域名的 A 紀錄及 CNAME,我要定義域名原本是 example.com 重定向為 www.example.com 網址,然後在 nginx/sites-available/example.conf 文件配置如下:

server {
    listen       80;
    server_name  www.example.com;
    root         /var/www/example.com;
}

在配置文件的最下面添加以下配置

server {
    listen       80;
    server_name  example.com;
    return 301 $scheme://www.example.com$request_uri;
}
Nginx

還有一種配置方法:

# 1. 專門處理不帶 www 的請求,並 301 重定向到帶 www 的網址
server {
    listen 80;
    server_name example.com;
    
    # 進行 301 永久轉址,並保留後方的 URI 路徑
    return 301 $scheme://www.example.com$request_uri;
}

# 2. 真正處理網站請求的主區塊 (帶 www)
server {
    listen 80;
    server_name www.example.com;

    root /var/www/example.com/htdocs;
    index index.php index.html index.htm;

    # 下方為您原本正常的網站配置,例如 WordPress 的規則
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
    # ...其他配置...
}
Nginx

包含 HTTPS (SSL) 的完整化配置:

現在的網站基本上都有 SSL 憑證,因此最標準的做法是將「所有 HTTP 請求」以及「不正確的 HTTPS 網域」全數轉向到 正確的 HTTPS 網域。

以下是將「不帶 www 的 HTTP / HTTPS」與「帶 www 的 HTTP」,全部統一 301 重定向到 https://www.example.com 的完整寫法:

# 1. 處理所有 HTTP (Port 80) 的請求,強制 301 轉向到 HTTPS 的 www
server {
    listen 80;
    server_name example.com www.example.com;
    
    return 301 https://www.example.com$request_uri;
}

# 2. 處理 HTTPS (Port 443) 但「不帶 www」的請求,301 轉向到 HTTPS 的 www
server {
    listen 443 ssl;
    server_name example.com;

    # 請替換為您實際的 SSL 憑證路徑
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    return 301 https://www.example.com$request_uri;
}

# 3. 真正處理網站內容的主區塊 (HTTPS + www)
server {
    listen 443 ssl;
    server_name www.example.com;

    # 請替換為您實際的 SSL 憑證路徑
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    root /var/www/example.com/htdocs;
    index index.php index.html index.htm;

    # 正常的網站 location 配置...
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
}
Nginx

HTTP 301 redirect HTTPS

另外補上其他重定向的配置,http 301 重定向到 https 配置:

域名加上 SSL 證書後,nginx.conf 一定不要忘記加上這個 301 redirect 配置。
server {
	listen 80;
        listen [::]:80;
	server_name www.example.com example.com;
	return 301 https://www.example.com$request_uri;
}
Nginx

301 重定向新網址

oldexample.com 301 重定向新網址 newexample.com

Nginx

server {
server_name example.com www.example.com;
return 301 $scheme://newdomain.com$request_uri;
}
Nginx

最後不要忘記重啟 Nginx

nginx -t && systemctl reload nginx
Nginx

.htaccess on Apache

#Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://newdomain.com/$1 [R=301,L]
Nginx

Kinsta 控制台

先在 MyKinsta 控制台安裝免費的 Let’s Encrypt 證書,然後利用 redirect tool 來做重定向。

olddomain.com - ^(.*)$ - https://newdomain.com$1
Nginx

2017/09/12 補充:

接手一個 WordPress 已經建置好了搬遷主機的案子,一般正常的 WordPress 後台就可以設定域名要不要有 www,但是很多外掛因素會造成域名轉向 (redirect) 的問題。

所以只能從主機端來執行,將配置寫入 /etc/nginx/sites-available/domain.com

例如:不帶 www 域名 301 轉址 (redirect) 到有 www 的域名

root /var/www/domain.com/htdocs;
if ($host != www.domain.com) {  return 301 $scheme://www.domain.com$request_uri;  }
Nginx

例如:有帶 www 域名 301 轉址 (redirect) 到沒有 www 的域名

root /var/www/domain.com/htdocs;
if ($host != domain.com) {  return 301 $scheme://domain.com$request_uri;  }
Nginx

Nginx 重啟後,域名有沒有 www 就會由主機端的配置來控制了。

Apache 系統

www URL 重定向到非 no-www

可以通過修改網站的 .htaccess 文件將 www.yourdomain.com 域名的所有請求重定向到 yourdomain.com。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]
Nginx

另一個是非 no-www URL 重定向到 www

可以通過修改網站的 .htaccess 文件將 yourdomain.com 域名的所有請求重定向到 www.yourdomain.com。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
Nginx

參考文件:

Nginx 官方 Rewrite 文檔:

https://nginx.org/en/docs/http/ngx_http_rewrite_module.html

Image

Sign up for free content.

我也不喜歡郵件垃圾,隨時可已取消訂閱。

Comments

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Your Mastodon Instance