Nginx 301 redirect matching directories

February 11, 2015 3.9k 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?

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.

Have another answer? Share your knowledge.