How to configure address processing so that URLs like /catalog/razdel/element/ change to /razdel/element/ ?
- oneregex101.com/r/zO2xE4/2 - is this what you need? - Sasha Chernykh
- Yes, thank you, come in handy - Nikolay
- onePodpokratil - regex101.com/r/zO2xE4/5 . - Sasha Chernykh
1 answer
It helps to split the model into subexpressions enclosed in parentheses - () . Suppose the site address is http://SashaGoddess.com . Let's try the following pattern:
Search : (http:\/\/SashaGoddess\.com\/)(.*?\/)(.*?\/.*?\/)
Replacement : $1$3
Demonstration : https://regex101.com/r/lR5pJ1/1
Let's sort it out:
- The first parentheses contain the name of the site,
- Secondly, its first section, i.e., the text bounded by front slips on the front and back -
/, - In the third - 2 subsequent sections of the site.
$1$3means that the replacement will be made for the contents inside the first and third brackets. What is inside the second will be removed. Depending on the implementation of regular expressions,\may be used instead of the$metacharacter$.
As a result, the address http://SashaGoddess.com/catalog/razdel/element/ will be replaced by http://SashaGoddess.com/razdel/element/ .
This pattern will remove the names of any sections in the root folder of the site. For example, as you can see from the example, the address http://SashaGoddess.com/Dream/Miracle/Destiny/ will contact http://SashaGoddess.com/Miracle/Destiny/ . If the URL only needs to get rid of the following following site name of the catalog section, modify the expression:
Search : (http:\/\/SashaGoddess\.com\/)(catalog\/)(.*?\/.*?\/)
Replacement : $1$3
Demonstration : https://regex101.com/r/lR5pJ1/2
In comparison with the previous model instead of metacharacters .*? (any characters except line breaks, repeated 0 or more times, the lazy mode is turned on) appeared catalog literals. There will be a replacement http://SashaGoddess.com/catalog/razdel/element/ → http://SashaGoddess.com/razdel/element/ , and the URL where the first section is not called catalog , like http://SashaGoddess.com/Dream/Miracle/Destiny/ will remain the same.