Nginx之URL重定向 Nginx

mowen 2019-09-21 2885

    # 带参数跳转(点击)
    if ($query_string ~* "i=(\d+)&a=v$") {
    	set $i $1;
    	rewrite (?i)^/views.php /arch/index.php?url=CCFunc/views/$i/v? last;
    }
    
    # 带参数跳转(搜索)
    if ($query_string ~* "^s=([^?]+)$") {
    	set $i $1;
    	rewrite (?i)^/ /arch/index.php?url=CCDynamicPage/search/s=$i/? last;
    }
    
    # 搜索
    rewrite (?i)^/search/([^?]+)/page/(\d+) /arch/index.php?url=CCDynamicPage/search/s=$1/p=$2 last;  
        
    
    # 首页重定向到/page/1.html
    rewrite (?i)^/$ /page/1.html last;
    
    # 分类首页
    rewrite (?i)^/([a-z-A-Z]+)$ /cate/$1/1.html last;
    rewrite (?i)^/([a-z-A-Z]+)/$ /cate/$1/1.html last;
    
    # 分类
    # /WWW/page/DDD 或 /WWW/page/DDD/,重定向到/cate/WWW/DDD.html
    rewrite (?i)^/([a-z-A-Z]+)/page/(\d+)$ /cate/$1/$2.html last;
    rewrite (?i)^/([a-z-A-Z]+)/page/(\d+)/$ /cate/$1/$2.html last;
    
    # 标签首页
    rewrite (?i)^/tag/([a-z-A-Z]+)$ /tag/$1/1.html last;
    rewrite (?i)^/tag/([a-z-A-Z]+)/$ /tag/$1/1.html last;
    
    # 标签
    # /WWW/tag/DDD 或 /WWW/tag/DDD/,重定向到/tag/WWW/DDD.html
    rewrite (?i)^/tag/([a-z-A-Z]+)/page/(\d+)$ /tag/$1/$2.html last;
    rewrite (?i)^/tag/([a-z-A-Z]+)/page/(\d+)/$ /tag/$1/$2.html last;
    
	# 列表首页
    # /page/DDD 或 /page/DDD/,重定向到/page/DDD.html
    rewrite (?i)^/page/(\d+)$ /page/$1.html last;
    rewrite (?i)^/page/(\d+)/$ /page/$1.html last;
    
    # 列表
    # /page/DDD/DDD 或 /page/DDD/DDD/,重定向到/page/DDD/DDD.html
    rewrite (?i)^/page/(\d+)/(\d+)$ /page/$1/$2.html last;
    rewrite (?i)^/page/(\d+)/(\d+)/$ /page/$1/$2.html last;
    
    # 详细页面首页
    # /DDD 或 /DDD/,重定向到/details/DDD/1.html
    rewrite (?i)^/(\d+)$ /details/$1/1.html last;
    rewrite (?i)^/(\d+)/$ /details/$1/1.html last;
    
    # 详细页面
    # /DDD/DDD 或 /DDD/DDD/,重定向到/details/DDD/DDD.html
    rewrite (?i)^/(\d+)/(\d+)$ /details/$1/$2.html last;
    rewrite (?i)^/(\d+)/(\d+)/$ /details/$1/$2.html last;
    
    # 如果不包含/public/,/arch/XXX/XXX => /arch/index.php?url=XXX/XXX
    if ($request_uri  !~*  /public/) {
    	rewrite (?i)^/arch/(\w+/\w+) /arch/index.php?url=$1 last;
    }
    
    # 如果不是文件或文件夹,/arch/public/XXX => /arch/public/XXX.html
    #if (!-e $request_filename){
    	rewrite (?i)^/arch/public/(\w+) /arch/public/$1.html last;
    #}

   

1、直接在server节点写即可;


2、其中(?i)表示不区分大小写;last表示隐藏重定向后的URL;redirect表示显示重定向后的URL。

     小技巧就是先写redirect,如果重定向后的URL正确,再改redirect为last。

最新回复 (0)
返回
发新帖
X