Score:0

Nginx: 301 redirect of specific .php file to directory

gb flag

I am switching my web server from Apache to Nginx, so I'm pretty new to Nginx. I thought I had the 301 redirect down fine, but I'm having an issue with redirecting an old url /game-of-foobar.php to /games/foobar/. Here is my server block below:

server {
    server_name www.mysite.com;
    root /home/mysite/mysite.com/;
    charset utf-8;

    listen [::]:443 http2 ssl; # managed by Certbot
    listen 443 http2 ssl; # managed by Certbot

    location / {
      try_files $uri $uri/ =404;
    }
    location /blog {
      try_files $uri $uri/ /blog/index.php?q=$uri$args;
    }
    location /blog/wp-admin {
      try_files $uri $uri/ /blog/wp-admin/index.php?$args;
    }

    location /game-of-foobar.php {
      return 301 https://www.mysite/games/foobar/;
    }

    location ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_pass unix:/var/run/php/php8.1-fpm-mysite.sock;
   }

    ssl_certificate ...
    ssl_certificate_key ...
}

When I visit "/game-of-foobar.php", I receive a 404 error. What is the correct way to do this?

Thanks

Richard Smith avatar
jp flag
See [how Nginx processes a request](http://nginx.org/en/docs/http/request_processing.html#simple_php_site_configuration). You should use `location = /game-of-foobar.php`
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.