0

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

category : example.com/index.php?category=mario-games > example.com/category/mario-games
game details : example.com/index.php?game=mario-game > example.com/mario-game.html
featured : example.com/featured.php > example.com/featured
pages : example.com/page.php?id=contact > example.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  example.com;
    return       301 http://www.example.com$request_uri;
}

server {
    listen       80;
    server_name  www.example.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;
    }


}
Dennis Nolte
  • 2,881
  • 4
  • 27
  • 37
Blithe
  • 3
  • 2

2 Answers2

0

I would make the redirections inside your index.php, something like this:

if (isset($_GET['category'])) {
    header('HTTP/1.1 301 Moved Permanently'); 
    header("Location: http://www.domain.com/category/" . $_GET['category']);
}

However, it could work something like this with nginx (I haven't tested a solution like this myself):

location = /index.php {
    if ($arg_category ~ (.+)) {
        return 301 http://www.domain.com/category/$1;
    }
    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;
}

So, we match the PHP file, and then test the query argument, capturing it into a regex for use with the return directive.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
0

You can't do both. The purpose of rewriting is so you can accept requests from different paths. The purpose of redirecting is to deflect those requests to where you want them to go, including a response to the client to always go there instead (response code 301). Trying to do both may not make sense and isn't possible because, once one is done, the client is already on his way elsewhere.

Rob
  • 344
  • 3
  • 15