简体   繁体   中英

IIS web.config rewrite/redirect rule with query string

I'm trying to setup a rewrite/redirect rule on a web.config file. I have little to no experience with IIS servers so I've been looking through solutions online, here at StackOverflow and other forums, and already tried a number of them without success.

I must point out that I only have FTP access to the server so I can't say for sure how it has been setup (in case server setup has any influence on this).

Here are the requirements:

  1. The page being redirected can have or not ".php" , ie, it can either be "my-page" or "my-page.php".
  2. The destination of the redirect is in a different domain/sub-domain
  3. The page URL itself has to be quite specific (besides ".php"). For example, I have two pages, "my-page.php" and "my-page-extra.php", but I only want to redirect "my-page.php".
  4. The query string present in the original can have multiple parameters in different order (or none at all)
  5. I want the query string to be passed as is to the destination.

So, a typical redirect would go like this: The visitor tries to reach:

https://my-domain.com/my-page.php?id=myid&param=myparam

or

https://my-domain.com/mypage?id=myid&param=myparam

And I want it redirected to:

https://my-sub-domain.my-new-domain.com/?id=myid&param=myparam

I have two solutions that work partially:

Solution 1: the URL works but the query string is not being passed

<rule name="My Redirect" stopProcessing="true">
    <match url="^my-page-slug(|\.php)(|\?.*)$" />
    <conditions logicalGrouping="MatchAll">
        <add input="{QUERY_STRING}" pattern="(.*)" />
    </conditions>
    <action type="Redirect" url="https://my-sub-domain.my-new-domain.com/{C:1}" redirectType="Permanent" />
</rule>

Solution 2: the query string is passed but the URL is not as strict so it doesn't work

<rule name="My redirect" stopProcessing="true">
    <match url="^(.*)my-page-slug(.*)$"  ignoreCase="true" />
    <action type="Redirect" url="https://my-sub-domain.my-new-domain.com/{R:1}" redirectType="Permanent" />
</rule>

Any help would be highly appreciated. Thanks.

You can try this rule:

<rule name="redirectrule1" stopProcessing="true">
   <match url="^my-page(|\.php)$" />
      <conditions>
         <add input="{HTTP_HOST}" pattern="^my-domain.com$" />
         <add input="{HTTPS}" pattern="on" />
      </conditions>
   <action type="Redirect" url="https://my-sub-domain.my-new-domain.com/" />
</rule> 

Thank you all for your help. After a lot of trial and errors, this is what worked for me (I'm pretty sure there are better ways of accomplishing this using conditions but this has done the trick so far):

<rule name="My Redirect" stopProcessing="true">
    <match url="^my-page(|\.php)(|\?.*)$" />
    <action type="Redirect" url="https://my-sub-domain.my-new-domain.com/{R:2}" appendQueryString="true" redirectType="Permanent" />
</rule>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM