2

I'm trying to understand how to do this, but seems very complicated (at least for someone like me having no experience with IIS but only Apache). I'm porting a website from Linux to a Windows server, and on linux server I have an .htaccess that helps me rewriting urls to hide pages and arguments:

RewriteRule ^store/([0-9a-zA-Z]+)$ http://www.domain.com/store/$1/ [R]

RewriteRule ^store/([0-9a-zA-Z]+)/$ http://www.domain.com/pages/store.asp?name=$1

RewriteRule ^store/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ http://www.domain.com/store/$1/$2/ [R]

RewriteRule ^store/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$ http://www.domain.com/pages/store.asp?name=$1&page=$2

So that when someone visits http://www.domain.com/store/client1/ is visiting http://www.domain.com/pages/store.asp?name=client1 (and so on with up to 4 arguments), but in the browser addressbar the url shown is still http://www.domain.com/store/client1/

I can't find my way to do the same on IIS 7...I made something like below:

            <rule name="Rule 5">
                <match url="^store/([0-9a-zA-Z]+)$" ignoreCase="false" />
                <action type="Redirect" url="http://www.domain.com/store/{R:1}/" redirectType="Found" />
            </rule>
            <rule name="Rule 6">
                <match url="^store/([0-9a-zA-Z]+)/$" ignoreCase="false" />
                <action type="Redirect" url="http://www.domain.com/pages/store.asp?name={R:1}" appendQueryString="false" redirectType="Found" />
            </rule>
            <rule name="Rule 7">
                <match url="^store/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$" ignoreCase="false" />
                <action type="Redirect" url="http://www.domain.com/store/{R:1}/{R:2}/" redirectType="Found" />
            </rule>
            <rule name="Rule 8">
                <match url="^store/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/$" ignoreCase="false" />
                <action type="Redirect" url="http://www.domain.com/pages/store.asp?name={R:1}&amp;page={R:2}" appendQueryString="false" redirectType="Found" />
            </rule>

This works for redirecting, so if I call www.domain.com/store/arg1/arg2/ I'm visiting www.domain.com/pages/store.asp?name={R:1}&page={R:2} , but in the browser addressbar I see the redirected address store.asp?name={R:1}&page={R:2} instead of the original www.domain.com/store/arg1/arg2/ , that instead what I need.

Is there a way to do this? I spent already several hours without a working solution...

opoloko
  • 347
  • 5
  • 13

1 Answers1

2

Instead of using an action of type Redirect, use Rewrite. See this blog post for more information. This will rewrite the URL on the server to the one that you desire instead of redirecting the browser to a new URL.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • Is there a way to persist the rewrite/redirects listed in the web.config across new builds/deployments using IIS? – feltspar Mar 28 '22 at 13:36