douzhanrun0497 2015-05-30 11:23
浏览 53

Nginx都重写和重定向?

I have arcade gaming web site. I want to convert my urls to sef.

category : domain.com/index.php?category=mario-games > domain.com/category/mario-games
game details : domain.com/index.php?game=mario-game > domain.com/mario-game.html
featured : domain.com/featured.php > domain.com/featured
pages : domain.com/page.php?id=contact > domain.com/page/contact

After searched Internet, I sucsessfully achieve rewrite rules. But my old urls still accessible. How can I both redirect and rewrite old urls to new ones ?

server {
    listen       80;
    server_name  domain.com;
    return       301 http://www.domain.com$request_uri;
}

server {
    listen       80;
    server_name  www.domain.com;
    root   /usr/share/nginx/domain;

    index  index.php index.html index.htm;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;


        location / {
        rewrite ^/(.*).html$ /index.php?game=$1 last;
        }
        location /category {
        rewrite ^/(.+)/$ /$1 permanent;
        rewrite ^/category/(.*)$ /index.php?category=$1 last;
        }
        location /page {
        rewrite ^/(.+)/$ /$1 permanent;
        rewrite ^/page/(.*)$ /page.php?id=$1 last;
        }
        location /featured {
            rewrite ^/(.+)/$ /$1 permanent;
            rewrite ^/(.*) /featured.php last;
        }






    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    }

    location ~ \.php$ {
        try_files  $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }


    location = /favicon.ico { log_not_found off; access_log off; }
    location = /robots.txt  { log_not_found off; access_log off; }

    location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        expires max; log_not_found off; access_log off;
    }
    location ~* \.(js|jpg|png|css)$ {

        expires 30d;
    }


}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
    • ¥15 unity连接Sqlserver
    • ¥15 图中这种约束条件lingo该怎么表示出来
    • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
    • ¥15 流式socket文件传输答疑
    • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
    • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
    • ¥15 win10,这种情况怎么办
    • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
    • ¥100 在连接内网VPN时,如何同时保持互联网连接