I'm trying to build a regular expression that matches the following:
This is the default English website URL: https://www.example.com/buy/apartment
This is the default German website URL: https://www.example.com/de/kauf/appartement
The expression I now have: ^([az]{0,2})\\/(buy|kauf)\\/([a-zA-Z0-9-+]+)$
So, breakdown of what I tried:
([az]{0,2})
to be used a parameter value of language
\\/
(buy|kauf)
, no need to use this value as a parameter, so not sure if I even have to use parentheses\\/
([a-zA-Z0-9-+]+)
to be used a parameter value for type
My IIS rewrite rule:
<rule name="test rewrite">
<match url="^([a-z]{0,2})\/(buy|kauf)\/([a-zA-Z0-9-+]+)$" />
<action type="Rewrite" url="homes.aspx?type={R:3}&language={R:1}" />
</rule>
I've been trying to test it here: https://regex101.com/ But I'm not getting a match on either these URL, where I would want them both to match: https://www.example.com/buy/apartment or https://www.example.com/de/kauf/appartement
URLs that should NOT match: https://www.example.com/en/all-objects or https://www.example.com/contact
UPDATE 1
I'm now down to this expression: https:\\/\\/(.*)?\\/([az]{0,2})\\/?(buy|kauf)\\/([a-zA-Z0-9-+]+)$
See https://regex101.com/r/pieJWW/1
However, I seem to be unable to get the language parameter "de" in the 2nd URL as a group.
UPDATE 2 In IIS web.config I don't use the part https://www.example.com/
to match on, but rather the part after that. So based on @Ryszard Czech's answer I now have this, but I can't grab the lang parameter, neither with {R:1}
or {R:2}
<rule name=""test rewrite">
<match url="^(([a-z]{0,2})\/)?(buy|kauf)\/([^\/]+)$" />
<action type="Rewrite" url="homes.aspx?type={R:4}&language={R:1}" />
</rule>
Use
^https:\/\/([^\/]+)\/(?:([a-z]{1,2})\/)?(buy|kauf)\/([^\/]+)$
See proof
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
https: 'https:'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^\/]+ any character except: '\/' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
[a-z]{1,2} any character of: 'a' to 'z' (between
1 and 2 times (matching the most
amount possible))
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
buy 'buy'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
kauf 'kauf'
--------------------------------------------------------------------------------
) end of \3
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
( group and capture to \4:
--------------------------------------------------------------------------------
[^\/]+ any character except: '\/' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \4
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.