regex to replace one word in url - 301 Redirect using URL Rewrite IIS asp.net -


i want set 301 redirect

mysite.com/banana mysite.com/fruit

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

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

(the words "yellow" , "sale" replaced anything, variables in route.)

this works first case, not others:

<rule name="banana" stopprocessing="true">   <match url="banana" />   <action type="redirect" url="fruit" /> </rule> 

based on question: replace underscore dash using url rewrite module of iis

this works third case only:

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

what need regex returns

{r:1} mysite.com/

{r:2} /yellow/sale

or

{r:1} mysite.com/

{r:2}

why not keep simple?

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

or if want go 2 levels deep:

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

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -