Regex 正则表达式与url中的单个文件夹匹配,以进行web.config url重写
我们目前使用一个相当垃圾的正则表达式(显然)来匹配url后的用户名,从而将它们重写到配置文件页面Regex 正则表达式与url中的单个文件夹匹配,以进行web.config url重写,regex,url-rewriting,web-config,Regex,Url Rewriting,Web Config,我们目前使用一个相当垃圾的正则表达式(显然)来匹配url后的用户名,从而将它们重写到配置文件页面 <rule name="Profiles" stopProcessing="true"> <match url="(?:\w|[-_.])+" ignoreCase="false" /> <conditions logicalGrouping="MatchAll" trackAllCaptures="fal
<rule name="Profiles" stopProcessing="true">
<match url="(?:\w|[-_.])+" ignoreCase="false" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{DOCUMENT_ROOT}/{R:0}" matchType="IsFile" negate="true" />
<add input="{DOCUMENT_ROOT}/{R:0}.php" matchType="IsFile" negate="true" />
<add input="{DOCUMENT_ROOT}/{R:0}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/profile.php?username={R:0}" appendQueryString="true" />
</rule>
允许字母数字和下划线,但不允许任何附加斜杠
例如,
/qweqwe/qweqwe
将失败。您的正则表达式与斜杠不匹配。如果只需要匹配单词字符,请使用\w+
,而不是(?:\w |[-.]])+
。也许,您还想锚定正则表达式,^\w+$
。也许,您实际上只需要“^(?::\w[-.]+$”
(或其等价的“^[-.\w]+$”
)?Chris,有什么消息吗?上面的注释有用吗?不幸的是没有:(好的,我假设正则表达式得到的字符串没有首字母/
,因此,为了将一个字符串与可选的尾随/
,您可以尝试“^[-\uw]+/?$”
或更通用的“^[^/]+/?$”
/myusername
/myusername/