VHosts and Directives, Part 6: Location Directive return and rewrite

Location Directives return and rewrite

In this article, I will explain, you how to return codes based on a URL location using the rewrite and return directive in the Nginx web server.

Move to Nginx configuration files directory.

cd /etc/nginx/vhost.d
ll

1.) Return 404

Add following code inside server block of your domain configuration file

location /forum {
return 404;
}

By using this you URL/forum will return 404 error page.

2.) Redirect using 301

location /forum {
return 301 http://google.com;
}

This will redirect your URL/forum page to Google using 301 error.

3.) Rewrite Url

location /forum {
rewrite ^/forum/(.*)$ http://www.myexample.local/forums/$1 permanent;
}

The above code will change your URL/forum to URL/forums.

 

If you find this post helpful, please share it on your social networks. If you have any questions? Leave a comment below, or you can contact me using the contact us page.

Add Comment