Nginx Location Rewrite

Nginx Rewrite 規則寫法

Written by

Photo by Evie S. on Unsplash

rewrite 功能就是,使用 Nginx 提供的全局變量或自己設置的變量,結合正則表達式和標誌位實現 url 重寫以及重定向。

rewrite 只能放在 server{}, location{}, if{} 中,並且只能對域名後邊的除去傳遞的參數外的字符串起作用;也就是說,這是美化網址的一個功能,讓讀者容易看懂網址,而不是一長串有 ? = & % 這些符號的網址。

例如,沒有經過 rewrite 規則寫法:

https://ds.gtour.info/forum.php?mod=viewthread&tid=31&extra=page%3D1

上面的網址經過 rewrite 規則寫法後:

https://ds.gtour.info/thread-31-1-1.html

這樣的網址看起來,就好看多了。

以 EasyEngine 來說,WordPress 程序已經預設在 conf 文件內了,但其他的程序就要額外將 Rewrite 規則存為 conf 文件,上傳到 /var/www/domain.com/conf/nginx 資料夾下。

檢查配置是否正確,重啟 Nginx

nginx -t && service nginx restart

這裡是在網路上收集的 rewrite 規則寫法,經過測試確實可用的:

DiscuzX

rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/index.php?action=$2&value=$3 last;
rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
#if (!-e $request_filename) {
#	return 404;
#}

drupal

if (!-e $request_filename) {
    rewrite ^/(.*)$ /index.php?q=$1 last;
    }

ecshop

if (!-e $request_filename)
{
    rewrite "^/index\.html" /index.php last;
    rewrite "^/category$" /index.php last;
    rewrite "^/feed-c([0-9]+)\.xml$" /feed.php?cat=$1 last;
    rewrite "^/feed-b([0-9]+)\.xml$" /feed.php?brand=$1 last;
    rewrite "^/feed\.xml$" /feed.php last;
    rewrite "^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$" /category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5&page=$6&sort=$7&order=$8 last;
    rewrite "^/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)(.*)\.html$" /category.php?id=$1&brand=$2&price_min=$3&price_max=$4&filter_attr=$5 last;
    rewrite "^/category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$" /category.php?id=$1&brand=$2&page=$3&sort=$4&order=$5 last;
    rewrite "^/category-([0-9]+)-b([0-9]+)-([0-9]+)(.*)\.html$" /category.php?id=$1&brand=$2&page=$3 last;
    rewrite "^/category-([0-9]+)-b([0-9]+)(.*)\.html$" /category.php?id=$1&brand=$2 last;
    rewrite "^/category-([0-9]+)(.*)\.html$" /category.php?id=$1 last;
    rewrite "^/goods-([0-9]+)(.*)\.html" /goods.php?id=$1 last;
    rewrite "^/article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$" /article_cat.php?id=$1&page=$2&sort=$3&order=$4 last;
    rewrite "^/article_cat-([0-9]+)-([0-9]+)(.*)\.html$" /article_cat.php?id=$1&page=$2 last;
    rewrite "^/article_cat-([0-9]+)(.*)\.html$" /article_cat.php?id=$1 last;
    rewrite "^/article-([0-9]+)(.*)\.html$" /article.php?id=$1 last;
    rewrite "^/brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)\.html" /brand.php?id=$1&cat=$2&page=$3&sort=$4&order=$5 last;
    rewrite "^/brand-([0-9]+)-c([0-9]+)-([0-9]+)(.*)\.html" /brand.php?id=$1&cat=$2&page=$3 last;
    rewrite "^/brand-([0-9]+)-c([0-9]+)(.*)\.html" /brand.php?id=$1&cat=$2 last;
    rewrite "^/brand-([0-9]+)(.*)\.html" /brand.php?id=$1 last;
    rewrite "^/tag-(.*)\.html" /search.php?keywords=$1 last;
    rewrite "^/snatch-([0-9]+)\.html$" /snatch.php?id=$1 last;
    rewrite "^/group_buy-([0-9]+)\.html$" /group_buy.php?act=view&id=$1 last;
    rewrite "^/auction-([0-9]+)\.html$" /auction.php?act=view&id=$1 last;
    rewrite "^/exchange-id([0-9]+)(.*)\.html$" /exchange.php?id=$1&act=view last;
    rewrite "^/exchange-([0-9]+)-min([0-9]+)-max([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$" /exchange.php?cat_id=$1&integral_min=$2&integral_max=$3&page=$4&sort=$5&order=$6 last;
    rewrite "^/exchange-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$" /exchange.php?cat_id=$1&page=$2&sort=$3&order=$4 last;
    rewrite "^/exchange-([0-9]+)-([0-9]+)(.*)\.html$" /exchange.php?cat_id=$1&page=$2 last;
    rewrite "^/exchange-([0-9]+)(.*)\.html$" /exchange.php?cat_id=$1 last;
}

joomla

location / {
    try_files $uri $uri/ /index.php?$args;
}

laravel

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

opencart

location = /sitemap.xml {
    rewrite ^(.*)$ /index.php?route=feed/google_sitemap break;
    }

location = /googlebase.xml {
    rewrite ^(.*)$ /index.php?route=feed/google_base break;
    }

location / {
    # This try_files directive is used to enable SEO-friendly URLs for OpenCart
    try_files $uri $uri/ @opencart;
    }

location @opencart {
    rewrite ^/(.+)$ /index.php?_route_=$1 last;
    }

typecho

if (-f $request_filename/index.html){
    rewrite (.*) $1/index.html break;
    }
if (-f $request_filename/index.php){
    rewrite (.*) $1/index.php;
    }
if (!-e $request_filename){
    rewrite (.*) /index.php;
    }

WordPress

location / {
    try_files $uri $uri/ /index.php?$args;
    }
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

Chevereto

# Chevereto Nginx rewrite rules
# Make sure to place these inside your server{} block

# Disable access to .ht* files
location ~ /\.ht {
	deny all;
}

# Disable access to sensitive files in app path
location ~ /(app|content|lib)/.*\.(po|php|lock|sql)$ {
   deny all;
}

# Disable log on not found images + image replacement
location ~* (jpe?g|png|gif) {
	log_not_found off;
	error_page 404 /content/images/system/default/404.gif;
}

# Enable CORS header (needed for CDN)
location ~* \.(ttf|ttc|otf|eot|woff|woff2|css|js)$ {
	add_header Access-Control-Allow-Origin "*";
}

# Force serve upload path as static content (match your upload folder if needed)
location /images {}

# Route dynamic request to index.php
location / {
	try_files $uri $uri/ /index.php?$query_string;
}

SMF (Simple Machines Forum)

location /smf/ {
                index index.php;
                rewrite ^(/smf)/([-_!~*'()$a-zA-Z0-9]+)/[0-9]?/?$ $1/index.php?pretty%3Bboard=$2.0 last;
                rewrite ^(/smf)/([-_!~*'()$a-zA-Z0-9]+)/([0-9]*)/[0-9]?/?$ $1/index.php?pretty%3Bboard=$2.$3 last;

                rewrite ^(/smf)/([-_!~*'()$a-zA-Z0-9]+)/([-_!~*'()$a-zA-Z0-9]+)/[0-9]?/?$ $1/index.php?pretty%3Bboard=$2%3Btopic=$3.0 last;
                rewrite ^(/smf)/([-_!~*'()$a-zA-Z0-9]+)/([-_!~*'()$a-zA-Z0-9]+)/([0-9]*|msg[0-9]*|new)/[0-9]?/?$ $1/index.php?pretty%3Bboard=$2%3Btopic=$3.$4 last;
        }
hosted by kinsta

發佈留言

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

Your Mastodon Instance