Nginx 301 redirect matching directories

Posted February 11, 2015 9.6k views

I used to own a WP blog integrated into my shopping cart but had since abandoned it.
It has 3 WP categories (new-arrival, news, promotion)

nginx log still show crawlers trying to crawl my blog links: e.g.


I want to redirect matching (new-arrival | news | promotion) directories to my home page =>

location / {
rewrite ^/(new-arrival|news|promotion|feed)/(.*)$1 permanent;

IE => This page can’t be displayed
IE => my custom 404 page
FF/Chrome => my custom 404 page

What should be the correct set of 301 redirects?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

I’d try something along these lines:

        location ~ /(new-arrival|news|promotion|feed)/(.*) {
                return 301;

Adding $1 to the end of your your rewrite ($1) will pass on the full path in your rewrite which you don’t want if you just want to send the user to your homepage. Also you generally want to prefer using return rather than rewrites.