2

My aim is to create Friendly URLs for a website hosted on IIS 7.5.

The site has 3 pages with the following URLs:

  • http://www.example.com/contact.php?langid=1&title=contact
  • http://www.example.com/technology.php?langid=1&title=technology
  • http://www.example.com/clients.php?langid=1&title=clients

I'd like the URLs in this format :

  • http://www.example.com/1/contact/
  • http://www.example.com/1/technology/
  • http://www.example.com/1/clients

When I create the friendly URLs in the IIS wizard it uses the same URL pattern

^([^/]+)/([^/]+)/?$

for each of the rules. This just means all 3 URLs redirect to the same page.

the web.config file looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <clear />
            <rule name="RedirectUserFriendlyURL1" stopProcessing="true">
                    <match url="^clients\.php$" />
                    <conditions>
                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                        <add input="{QUERY_STRING}" pattern="^langid=([^=&amp;]+)&amp;title=([^=&amp;]+)$" />
                    </conditions>
                    <action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
                </rule>
                <rule name="RewriteUserFriendlyURL1" stopProcessing="true">
                    <match url="^([^/]+)/([^/]+)/?$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="clients.php?langid={R:1}&amp;title={R:2}" />
                </rule>
                <rule name="RedirectUserFriendlyURL2" stopProcessing="true">
                    <match url="^contact\.php$" />
                    <conditions>
                        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                        <add input="{QUERY_STRING}" pattern="^langid=([^=&amp;]+)&amp;title=([^=&amp;]+)$" />
                    </conditions>
                    <action type="Redirect" url="{C:1}/{C:2}" appendQueryString="false" />
                </rule>
                <rule name="RewriteUserFriendlyURL2" stopProcessing="true">
                    <match url="^([^/]+)/([^/]+)/?$" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="contact.php?langid={R:1}&amp;title={R:2}" />
                </rule>
</rules>
            <outboundRules>
                <clear />
                <rule name="OutboundRewriteUserFriendlyURL1" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^(.*/)clients\.php\?langid=([^=&amp;]+)&amp;(?:amp;)?title=([^=&amp;]+)$" />
                    <action type="Rewrite" value="{R:1}{R:2}/{R:3}/" />
                </rule>
                <rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
                    <match filterByTags="A, Form, Img" pattern="^(.*/)contact\.php\?langid=([^=&amp;]+)&amp;(?:amp;)?title=([^=&amp;]+)$" />
                    <action type="Rewrite" value="{R:1}{R:2}/{R:3}/" />
                </rule>
                <preConditions>
                    <preCondition name="ResponseIsHtml1">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
        <urlCompression doDynamicCompression="false" />
    </system.webServer>
</configuration>

Any suggestions how to do this correctly?

  • 1
    At a guess you should change the RewriteUserFriendlyURL2 action url to something like: {R:2}.php?langid={R:1}&amp;title={R:2} if title always equals the filename. – Zhaph - Ben Duguid Nov 2 '17 at 18:23
  • 1
    that's great, exactly what i was looking for.... if you want to put it as an answer i'll mark it as correct... thanks... – Dog Nov 5 '17 at 7:41
  • Cheers, was away from a computer when I commented so didn't want to answer without confirming it. – Zhaph - Ben Duguid Nov 5 '17 at 9:52
1

The issue is that the action is hard coded to use the same filename. You could add some additional conditions and duplicate the rules, but a simpler option would be to change the RewriteUserFriendlyURL2 action url to:

{R:2}.php?langid={R:1}&amp;title={R:2}

If the title parameter always equals the filename.

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.