Welcome! Log In Create A New Profile

Advanced

Redirect 301 only exact url and not all other sub urls

Posted by tarichecco 
Redirect 301 only exact url and not all other sub urls
May 23, 2017 05:36PM
Dear All,

I'have some urls like these:

https://www.mydomain.com/catalog/category1
https://www.mydomain.com/catalog/category1/product1
https://www.mydomain.com/catalog/category1/product2
https://www.mydomain.com/catalog/category1/product3
...etc...

I have to redirect 301 only

https://www.mydomain.com/catalog/category1
to
https://www.mydomain.com/catalog/newcategory1

and not the other urls.

But if I do

location /catalog/category1 {
return 301 /catalog/newcategory1;
}

nginx redirect all the sub urls with https://www.mydomain.com/catalog/category1 as root to https://www.mydomain.com/catalog/newcategory1

How can I do?

Thank you very much.

Tarichecco
Re: Redirect 301 only exact url and not all other sub urls
May 25, 2017 06:03AM
Each nginx request is handled by one and only one location. Since these requests match the location you've put, that's where they're handled.

You'll need to set up two locations, once for that specific URI, and one for the others.

See http://nginx.org/en/docs/http/ngx_http_core_module.html#location.

--
Jim Ohlstein
Re: Redirect 301 only exact url and not all other sub urls [SOLVED]
May 25, 2017 10:32AM
Hi Jim,
Thank you very much!

In LOCATION statement I forgot the equal operator "="

location = /catalog/category1 {
return 301 /catalog/newcategory1;
}

In this way redirect only the exact match with (https://www.mydomain.com/catalog/category1 ) and the other sub urls remains the same.

Before
https://www.mydomain.com/catalog/category1
https://www.mydomain.com/catalog/category1/product1
https://www.mydomain.com/catalog/category1/product2
https://www.mydomain.com/catalog/category1/product3

After
https://www.mydomain.com/catalog/newcategory1
https://www.mydomain.com/catalog/category1/product1
https://www.mydomain.com/catalog/category1/product2
https://www.mydomain.com/catalog/category1/product3
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 130
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready