1 minute reading time (226 words)

Nginx Rewrite Rule - 301 Redirect to Another Path for Multiple URLs

 How to create a 301 permanent redirect to another path for multiple URLs using the Nginx return directive

301 Moved Permanently

A 301 redirect is used to forward website traffic to a new URL.

If you change your website menu structure, you'll need to create a 301 redirect to forward web traffic to the new URL. If you don't, any search engine traffic going to the pages will be lost and links will be broken and people will get a 404 page Not Found error.

Example: Nginx 301 Redirect a Path to another URL

In Nginx, you can use the return directive inside a location block to specify URLs that need to be re-written
$1 is used as a variable for the part of the URL that isn't changing. In this case, the web page title.

In this example, the redirect location is inside the server https block

old URL https://yourdomain.com/categories/how-to-guides
new URL https://yourdomain.com/categories/guides

location ~ ^/categories/how-to-guides/(.*) {
        return 301 https://yourdomain.com/categories/guides/$1;
} 

Check your Nginx configuration

nginx -t

Reload Nginx

service nginx reload

For redirecting one page to another URL, see this guide:

Nginx Rewrite Rule - 301 Redirect One Page to Another URL
https://techlabs.blog/categories/guides/nginx-rewrite-rule-301-redirect-one-page-to-another-url

Reference:

Creating NGINX Rewrite Rules
https://www.nginx.com/blog/creating-nginx-rewrite-rules

Related Posts

 

Comments

No comments made yet. Be the first to submit a comment
Already Registered? Login Here
Saturday, 23 September 2023
You can help support this website by buying me a coffee!
Buy Me A Coffee