Cody Ng
Cody Ng

Reputation: 349

IIS7.5 web.config - How to match a path with query string?

I think it may be an old question but I need to make some redirections for clients' old sites. They are using IIS7.5.

The site has 3 languages and English is the default one, with the following URLs:

English: https://olddomain.com

Traditional Chinese: https://olddomain.com/index.php?q=zh-hant

Simplified Chinese: https://olddomain.com/index.php?q=zh-hans

Note that there are other sections of the site, such as "about-us". The old ugly structure is:

English: https://olddomain.com/index.php?q=about-us

Traditional Chinese: https://olddomain.com/index.php?q=zh-hant/about-us

Simplified Chinese: https://olddomain.com/index.php?q=zh-hans/about-us

Actually, just redirect all old site's home AND/OR sections to new site's home only is enough.

I have tried to add the and using :

        <httpRedirect enabled="true" exactDestination="true">
        <add wildcard="/index.php?q=zh-hans*" destination="https://newsitedomain.com/sc/somename" />
        <add wildcard="/index.php?q=zh-hant*" destination="https://newsitedomain.com/tc/somename" />
        <add wildcard="*" destination="https://newsitedomain.com/en/somename" />
    </httpRedirect>

The English version is fine but all other TC and SC version go to EN new site as well.

What's wrong with my config?

I've read the Microsoft docs: but it does not mention any handling for query string condition.

Many thanks

Upvotes: 0

Views: 302

Answers (1)

daniel
daniel

Reputation: 1070

Seems the IIS httpRedirect cannot handle the query string of the URL.

Here is a workaround:

<!-- redirect base URL -->
<system.webServer>
  <httpRedirect enabled="true" exactDestination="true">
    <add wildcard="/" destination="https://newsitedomain.com/en/somename" />
  </httpRedirect>
<system.webServer>
<!-- custom redirect by _redirect.html (or your custom php) -->
<location path="index.php">
  <system.webServer>
    <httpRedirect enabled="true" destination="_redirect.html$Q" exactDestination="true" />
  </system.webServer>
</location>

And you need the following _redirect.html file aside the web.config

<html>
  <head>
    <script>
      if (window.location.href.indexOf('q=zh-hans') !== -1) {
        window.location.href = 'https://newsitedomain.com/sc/somename';
      } else if (window.location.href.indexOf('q=zh-hant') !== -1) {
        window.location.href = 'https://newsitedomain.com/tc/somename';
      } else {
        window.location.href = 'https://newsitedomain.com/en/somename';
      }
    </script>
  </head>
</html>

Another option is using URL Rewrite module.

Upvotes: 1

Related Questions