How to redirect a single URL in Nginx?

How can I redirect a single URL to a new location?
Basically something like this:

http://www.admon.org/how-to-clear-all-bash-shell-aliases —> http://www.admon.org/applications/how-to-clear-all-bash-shell-aliases/

This can be done with the help of location directive in Nginx.

location /how-to-clear-all-bash-shell-aliases {
	rewrite ^/.* http://$server_name/applications/how-to-clear-all-bash-shell-aliases/ permanent;
}

By the way, as we have recently re-architectured our website http://www.admon.org/, all the URLs have updated and new URL contains a category name, then we can generate a list of relocation for all our URLs from website sitemap.xml.

Here is the command for reference,

for i in `cat url_list`; do echo locate /$(echo $i| awk -F/ '{print $5}') \{; echo -en '\t'; echo rewrite \^\/\.\* $(echo $i| sed 's/www.admon.org/\$server_name/g')  permanent\;; echo \}; done > admon_new.rules

Then we just need to include admon_new.rules in the virtual host config file.

Share Button

Leave a comment

Your email address will not be published. Required fields are marked *