skip to Main Content

I am trying to get a web redirect to work for my wordpress site, but nothing happens for the redirect url.

The site is wordpress and has an existing web.config file which looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
            <rule name="WordPress" patternSyntax="Wildcard">
                <match url="*"/>
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                    </conditions>
                <action type="Rewrite" url="index.php"/>
            </rule>
    </rules>
    </rewrite>
    <staticContent>
            <mimeMap fileExtension=".json" mimeType="application/json" />
     </staticContent>
  </system.webServer>
</configuration>

I want the url to redirect FROM /path/to/old TO /path/to/new and have added the following after the first rule:

<rule name="Redirect" stopProcessing="true">
  <match url="^path/to/new" />
  <action type="Redirect" url="http:/www.site.com/path/to/new" redirectType="Found"/>
</rule>

So the full code looks like this:

   <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
                <rule name="WordPress" patternSyntax="Wildcard">
                    <match url="*"/>
                        <conditions>
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
                        </conditions>
                    <action type="Rewrite" url="index.php"/>
                </rule>
               <rule name="Redirect" stopProcessing="true">
                 <match url="^path/to/new" />
                 <action type="Redirect" url="http:/www.site.com/path/to/new" redirectType="Found"/>
               </rule>
        </rules>
        </rewrite>
        <staticContent>
                <mimeMap fileExtension=".json" mimeType="application/json" />
         </staticContent>
      </system.webServer>
    </configuration>

I am new to web.config, but have used .htaccess before. A bit clueless here why it is not working – nothing happens on the site when I enter the URL that should be redirected.

2

Answers


  1. As far as I remember, the rules a prioritized by their order, meaning your wildcard rule processes all the requests before the specific rule and then the requests don’t match the pattern.

    Try placing the Redirect rule before the WordPress rule and remove the stopProcessing attribute.

    Hope it helps!

    Login or Signup to reply.
  2. According to the IIS documentation at
    https://learn.microsoft.com/en-us/iis/configuration/system.webserver/httpredirect/

    it looks like this can be as simple as putting

     <httpRedirect enabled="true" destination="http:/www.site.com/path/to/new" />
    

    to the root of <system.webServer> perhaps even before the rewrite mod. Can you give that a try and let us know if that works for you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search