2

I have some URLs as follows

  • http://localhost/wordpress/latest/joe/12
  • http://localhost/wordpress/latest/bloggs/14
  • http://localhost/wordpress/latest/thing/15

I would like to redirect them to

  • http://localhost/wordpress/file.php?id=12
  • http://localhost/wordpress/file.php?id=14
  • http://localhost/wordpress/file.php?id=15

So latest needs to be in the URL, then it could be anything, then a number

Any advice? I've tried this and although the test is fine, going to the URL doesn't work. I've even tried using a rewrite map but that doesn't work either.

By 'doesn't work' I mean the original URL stays on place.

<rule name="TEST" patternSyntax="ECMAScript" stopProcessing="true">
  <match url="/wordpress/latest/(.*)/(.*)" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
  <action type="Redirect" url="http://localhost/wordpress/file.php?id=$2" appendQueryString="false" />
</rule>

ReWrite Map

<rule name="ArticlesRule">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                        <add input="{Articles:{REQUEST_URI}}" pattern="(.+)" />
                    </conditions>
                    <action type="Rewrite" url="{C:1}" appendQueryString="false" />
                </rule></rules>
            <rewriteMaps>
                <rewriteMap name="Articles">
                    <add key="/wordpress/latest/dfjkdskfjdklsf/12" value="/wordpress/?id=12" />
                </rewriteMap>
            </rewriteMaps>
0

You want to capture the desired id of the article using a condition, then you want to use that id you captured in the redirect action using {C:2} or Condition variable 2. Note that REQUEST_URI holds the path and query string while PATH_INFO just holds the path without query string. You probably want the path without the query string when capturing the article id.

<rule name="ArticlesRule">
    <match url=".*" />
    <conditions logicalGrouping="MatchAll">
        <add input="{PATH_INFO}" pattern="^/wordpress/latest/(.+)/([0-9]+)" ignoreCase="true" />
    </conditions>
    <action type="Redirect" url="http://localhost/wordpress/file.php?id={C:2}" appendQueryString="false" />
</rule>

If you want any other query string parameters to pass through to the redirected url, you could add appendQueryString="true" to your <action> element

If you want the clean urls to remain in the browser's address bar and hide the fact that file.php is being referenced, you would change your <action type="Redirect"> to <action type="Rewrite">

Otherwise, this rule should redirect any /wordpress/latest/anything/anynumber to /wordpress/file.php?id=anynumber

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.