Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a WordPress blog hosted in my IIS virtual directory that has all URLs ending with a forward slash. For example:

http://www.example.com/blog/

I have the following rules defined in my web.config:

<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-domain-to-www" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="example.com" />
    </conditions>
    <action type="Redirect" url="http://www.example.com/blog/{R:0}" />
</rule>

In addition, I tried adding the following rule for removing trailing slashes:

<rule name="Remove trailing slash" stopProcessing="true">
    <match url="(.*)/$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>

It seems that the last rule doesn't work at all. Anyone around here who has attempted to remove trailing slashes from WordPress blogs hosted on IIS?

share|improve this question
    
Have you tried removing the stopProcessing="true" attribute from the Redirect-domain-to-www rule? That should make it process your remove trailing slash rule (which seems correct to me). –  Mathias R. Jessen Mar 28 '12 at 23:16
    
Yes, tried that but it didn't work... Anyhow read in a blog post that it was a bad idea to remove forward slashes. So I think I'll just have to give up. –  Zishan Mar 30 '12 at 22:46
add comment

1 Answer

WordPress will accept URL's both with or without a trailing slash. So no problem on the inbound side.

It just only generate URL's with a trailing slash so instead of using an inbound rewrite rule you should use an outbound rewrite rule to just remove the slashes if present.

Since outbound rules don't work together with output compression you have to disable that. Together with the standard rewrite rule for WordPress you end up with the following web.config:

<?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>
            <outboundRules>
                <rule name="Remove trailing slash" preCondition="IsHTML">
                    <match filterByTags="A, Form, IFrame" pattern="(.*)/$" />
                    <action type="Rewrite" value="{R:1}" />
                </rule>
                <preConditions>
                    <preCondition name="IsHTML">
                        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
        <urlCompression doStaticCompression="false" doDynamicCompression="false" />
    </system.webServer>
</configuration>

Hope this helps.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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