2014-06-09 5 views
1

합니다 (정규식 URL에 한 단어를 대체하는 - 301 리디렉션 사용하여 URL 재 작성 IIS의 asp.net 나는 301 리디렉션 설정하려는

mysite.com/banana mysite.com/fruit

mysite.com/banana/yellow mysite.com/fruit/yellow

mysite.com/banana/yellow/sale mysite.com/fruit/yellow/sale

에 대한 "노란색"과 "판매"라는 단어는 어떤 것으로도 대체 될 수 있지만 경로의 변수입니다.)

Th 이 질문을 바탕으로

<rule name="banana" stopProcessing="true"> 
    <match url="banana" /> 
    <action type="Redirect" url="fruit" /> 
</rule> 

: 이는 세 번째 경우 작동 replace underscore with dash using url rewrite module of iis

:

<rule name="banana" stopProcessing="true"> 
    <match url="(mysite.com.*/[^/]*?)banana/([^/_]*)/([^/_]*)$" /> 
    <action type="Redirect" url="{R:1}fruit/{R:2}/{R:3}" /> 
</rule> 

는 내가 필요로하는 것은 첫 번째 경우에 대한 작품,하지만 다른 사람이다 반환하는 정규식

{R : 1} mysite.com/

{R : 2}/노란색/판매

또는

{R : 1} mysite.com/

{R : 2}

답변

2

가 왜 간단하게?

<match url="mysite.com/banana(/.+)*$" /> 
<action type="Redirect" url="mysite.com/fruit{R:1}" /> 

또는 당신은 단지 깊은 두 가지 수준의 가고 싶은 경우 :

<match url="mysite.com/banana((?:/[^/]+){0,2})$" /> 
<action type="Redirect" url="mysite.com/fruit{R:1}" /> 
+0

첫 정규 표현식에서 미묘한 오류가있을 수 있습니다 - 때 첫 번째 두 샘플에 대해 대체가 발생하지 않는지 테스트하십시오. –

+0

둘 다 저에게 효과적입니다. – tenub

+0

제 테스트 도구가 $ 우물을 처리하지 못했습니다. 죄송합니다. –

1

방법에 대한 다음 :

<rule name="banana" stopProcessing="true"> 
    <match url="mysite.com/banana(.+)?" /> 
    <action type="Redirect" url="mysite.com/fruit{R:1}" /> 
</rule> 
+0

이 질문은 저에게도 도움이됩니다. 답변이 처음 나온 이후 답변을 하나만 표시했습니다. 감사! – scw