Question

Nginx 301 redirect matching directories

Posted February 11, 2015 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.

/public_html/new-arrival/
/public_html/new-arrival/launch-72/
/public_html/news/
/public_html/promotion/25-percent-off/

I want to redirect matching (new-arrival | news | promotion) directories to my home page => https://www.example.com

location / {
rewrite ^/(new-arrival|news|promotion|feed)/(.*) https://www.example.com/$1 permanent;
...
...
}

[results]
IE https://www.example.com/promotion/ => This page can’t be displayed
IE https://www.example.com/promotion => my custom 404 page
FF/Chrome https://www.example.com/promotion/ => 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.

×
1 answer

I’d try something along these lines:

        location ~ /(new-arrival|news|promotion|feed)/(.*) {
                return 301 https://www.example.com;
        }

Adding $1 to the end of your your rewrite (https://www.example.com/$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.

Submit an Answer