2015-02-05 76 views
1

我有一个要求,以更新的web.config文件,以便以后替换路径/和重写URL如下:RegExp的web.config中 - 后选择路径/和重写URL作为PARAM

域。 COM/acct01 - > domain.com/#/?id=acct01

domain.com/acct02 - > domain.com/#/?id=acct02

domain.com/acct03 - > domain.com /#/?id = acct03

...

目前,我为每个帐户添加以下内容到web.confg文件。因此,对于上面的例子中,我需要做到以下几点:

<rule name="acct01" stopProcessing="true"> 
    <match url="^acct01$" /> 
    <conditions> 
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="https://domain.com/#/?id=acct01" /> 
</rule> 

<rule name="acct02" stopProcessing="true"> 
    <match url="^acct02$" /> 
    <conditions> 
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="https://domain.com/#/?id=acct02" /> 
</rule> 

<rule name="acct03" stopProcessing="true"> 
    <match url="^acct03$" /> 
    <conditions> 
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="https://domain.com/#/?id=acct03" /> 
</rule> 

正如你所看到的,这样可以多帐户后,尾大不掉。我正在寻找一种捕获URL最后/内部值的常规方法,以及在id URL参数之后替换操作部分中的URL的更一般化方式。例如:

<rule name="addIDFromPath" stopProcessing="true"> 
    <match url="[REGEX FOR GENERAL EXTRACTION OF PATH AFTER LAST /]" /> 
    <conditions> 
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="https://domain.com/#/?id=[VALUE OF PATH EXTRACTED]" /> 
</rule> 

如果URL包含#,则应该忽略并单独保留,因为这表示它已被重写。

我们在安装了IIS的Windows Box上,因此可以考虑替代web.config文件。

谢谢!

回答

2

不知道如果我理解正确的问题,但是,如果这个想法是去年//#/?id=代替那么这个模式将做的工作,与/#/?id=取代

(?=\/([a-z0-9]+)$) 

你可以在这里进行测试:https://regex101.com/r/kW8zU0/1

编辑

大概是这样的:

<rule name="addIDFromPath" stopProcessing="true"> 
    <match url="(^|\/)([a-z0-9]+)(?=$)" /> 
    <conditions> 
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> 
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
    </conditions> 
    <action type="Redirect" url="https://domain.com/#/?id={R:2}" /> 
</rule> 

不确定组号码,可能是{R:1}

模式:(^|\/)([a-z0-9]+)(?=$)

演示:https://regex101.com/r/kW8zU0/4

+0

太棒了。该模式确实有效,但是如何在web.config文件中使用它,以便重写URL? – snazzyHawk 2015-02-05 15:30:29

+0

在你提供的url的web.config中已经有了正确的结尾:'#/?id = acct01'。你想要取代什么?你能否提供一些文件和期望输出的例子? – streetturtle 2015-02-05 15:34:40

+0

查看原始问题中配置文件的代码片段,找到的模式将进入并且替换将进入作为URL ID参数。我可能有100个这些帐户和上面的代码片段为每个帐户复制一次。 – snazzyHawk 2015-02-05 15:50:01