How can I match and redirect urls starting with something

Merchant
Frá Slovenia
Meðlimur síðan okt. 2023

About redirects...

[quote]Create a moved (HTTP 301) rule

Match: ^https?://([^/]*)/oldpage(\?|$)
Redirect: https://$1/newpage[/quote]
How can I create rule that will redirect all urls start with:

https://mydomain.com/kategorija-izdelka/
https://mydomain.com/izdelek/

For example:

https://mydomain.com/kategorija-izdelka/category-1
https://mydomain.com/kategorija-izdelka/category-2/subcategory

to:
https://mydomain.com/

Because I see in log that if url is not exactly https://mydomain.com/kategorija-izdelka, get redirected to https://mydomain.com/subcategory instead just to to https://mydomain.com.

tim
Founder
Frá Sweden
Meðlimur síðan maí 2013
tim

Short answer:
^https://www\.mydomain\.com/kategorija-izdelka/.*
^ match string beginning
. matches an actual dot
. match any character

  • continously

Some explainations:

To match urls starting with.
[b]^[/b]https://mydomain\.com/kategorija-izdelka/

To match both http and https, make s optional by succeeding it by a ?
^http[b]s?[/b]://mydomain.com/kategorija-izdelka/

To match both or without www., group it with a paranthesis and succeed it by a ? to make it optional:
^https?://b?[/b]mydomain.com/kategorija-izdelka/
(This group is captured and retrieved by $1 if you need it in your redirect url)

To match any domain, [^/] means anything that is not a slash / and means continously:
^https?://[b]([^/]
)[/b]/kategorija-izdelka/

To match an exact url, and nothing else preceeding or following.
[b]^[/b]https://mydomain\.com/kategorija-izdelka/[b]$[/b]

..and to make the last slash optional
^https://mydomain\.com/kategorija-izdelka[b]/?[/b]$

To match an exact url [u]with optional query parameters following[/u]. (\?|$) means match a question mark or the end of string.
[b]^[/b]https://mydomain\.com/kategorija-izdelka/?[b](\?|$)[/b]

Use this online tool for toying with regex:
https://regex101.com/

^https://mydomain\.com/kategorija-izdelka/ will turn https://mydomain\.com/kategorija-izdelka[b]/remainings[/b] into https://redirectdomain\.com[b]/remainings[/b].
While ^https://mydomain\.com/kategorija-izdelka/.*$ will turn https://mydomain\.com/kategorija-izdelka/remainings into https://redirectdomain\.com/.

Merchant
Frá Slovenia
Meðlimur síðan okt. 2023

@tim Thank you very much for detailed and easy explanation!

Þú
This website uses no cookies and no third party tracking technology. We think we can do better than others and really think about your privacy.