could you write example of nginx 301 redirect one location to other local url
- User
could you write example of nginx 301 redirect one location to other local url
- Assistant
Certainly! To set up an Nginx 301 (permanent) redirect from one location to another local URL, you can use the
returndirective. Here's an example configuration:Assume you want to redirect
http://example.com/old-pagetohttp://example.com/new-page.server { listen 80; server_name example.com; location /old-page { return 301 /new-page; } location /new-page { # This can be your new page configuration or any other configurations. try_files $uri $uri/ =404; } # You may have other location blocks or configuration as needed. }Explanation:
listen 80;specifies that the Nginx server will listen on port 80.server_name example.com;specifies the server name (your domain).- The
location /old-pageblock is the old URL that you want to redirect. return 301 /new-page;tells Nginx to issue a 301 redirect to .
- User
i have rule
location /print { return 301 $arg_site_path; }
but argument not working, and it redirecting to the same page again, if i have url like this https://domain.com/print?site_path=/news.html, so i get infinite loop, what is wrong?
- Assistant
The issue you're encountering is due to the fact that
$arg_site_pathcontains the/printpath in the query part (?site_path=/news.html), which is causing it to redirect to/news.htmlwhile still under the/printlocation block. This leads to an infinite loop.To resolve this, you can use a combination of
rewriteandreturndirectives or conditional logic to properly handle the redirection: