Report this

What is the reason for this report?

Nginx 301 redirect matching directories

Posted on February 11, 2015

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?



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.