3,077 瀏覽數

WordPress 固定網址設定(Nginx)

測試環境為 CentOS 7 x64 , Web Server 為 Nginx

預設文章網址是長成這樣 http://benjr.tw/?p=4 ,但是我們可以使用固定網址設定,可以讓你的網址變成這樣短 http://benjr.tw/4 ,不過不用擔心以前設定的 ?p= wordpress 還是會幫你轉到新的網址去.

Nginx

關於 Nginx 的安裝與設定可以參考

Nginx (HTTP 服務器) 須要設定過後才能使用.

[ben@localhost ~]$ sudo vi /etc/nginx/conf.d/default.conf 
[sudo] password for ben: 
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.php?$args; 
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

主要是加入 try_files $uri $uri/ /index.php?$args;

設定完可以檢查一下設定檔是否有輸入錯誤.

[ben@localhost ~]$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

還要重新啟動 Nginx

[ben@localhost ~]$ sudo systemctl restart nginx
[ben@localhost ~]$ sudo systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2017-04-05 10:54:37 CST; 5s ago
     Docs: http://nginx.org/en/docs/
  Process: 4716 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
  Process: 4725 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 4722 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 4728 (nginx)
   CGroup: /system.slice/nginx.service
           ├─4728 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.c...
           └─4729 nginx: worker process

Apr 05 10:54:37 localhost.localdomain systemd[1]: Starting nginx - high perfo...
Apr 05 10:54:37 localhost.localdomain nginx[4722]: nginx: the configuration f...
Apr 05 10:54:37 localhost.localdomain nginx[4722]: nginx: configuration file ...
Apr 05 10:54:37 localhost.localdomain systemd[1]: Started nginx - high perfor...
Hint: Some lines were ellipsized, use -l to show in full.

設定好之後可以到 控制台 / 設定 / 固定網址 / 常見設定 / 自訂結構 來設定.

在 WordPress 的設定 固定網址,我是設定成 自訂結構 http://benjr.tw/%post_id% , /%post_id% 代表文章網址將以 unique ID 來顯示比如 4,更多的設定參數請自行參考 Using Permalinks 儲存即可.

參數參考

  • %year%
    The year of the post, four digits, for example 2004
  • %monthnum%
    Month of the year, for example 05
  • %day%
    Day of the month, for example 28
  • %hour%
    Hour of the day, for example 15
  • %minute%
    Minute of the hour, for example 43
  • %second%
    Second of the minute, for example 33
  • %post_id%
    The unique ID # of the post, for example 423
  • %postname%
    A sanitized version of the title of the post (post slug field on Edit Post/Page panel). So “This Is A Great Post!” becomes this-is-a-great-post in the URI.
  • %category%
    A sanitized version of the category name (category slug field on New/Edit Category panel). Nested sub-categories appear as nested directories in the URI.
  • %author%
    A sanitized version of the author name.

發表迴響