skip to Main Content

I am using URL Rewrite to force a http to https redirect. It is working, except it is adding a second trailing slash to the URL. So for example, if I go to http://example.com, the rewritten URL ends up being https://example.com//. I don’t want even 1 trailing slash, let alone 2 and I cannot get rid of it. I tried this solution but it didn’t work. Might be because the person wanted to get rid of the trailing slash, and I have 2 of them so it isn’t matching?

I have the below rules in my web.config file. I am running Windows Server 2019 with IIS 10 and the URL Rewrite Module. If someone can show me where I am going wrong, I’d appreciate it!

<rule name="HTTPS Redirect" stopProcessing="true">
    <match url="(.*)" />
        <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
        </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{REQUEST_URI}" appendQueryString="false" />
</rule>
<rule name="SEO - Remove trailing slash" stopProcessing="false">
    <match url="(.+)/$" />
        <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_METHOD}" pattern="GET" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        </conditions>
    <action type="Rewrite" url="_{R:1}" />
</rule>

3

Answers


  1. You can try to use this URL Rewrite rule:

    <rule name="HTTPS Redirect" stopProcessing="true">
        <match url="^(.*)$" />
            <conditions>
                <add input="{HTTPS}" pattern="^OFF$" />
            </conditions>
        <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" appendQueryString="false" />
    </rule>
    <rule name="SEO - Remove trailing slash" stopProcessing="false">
        <match url="(.+)/$" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_METHOD}" pattern="GET" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            </conditions>
        <action type="Rewrite" url="_{R:1}" />
    </rule>
    
    Login or Signup to reply.
  2. Just remove the slash between {HTTP_HOST} and {REQUEST_URI} in the action Redirect URL.
    (https://serverfault.com/questions/893315/best-way-to-redirect-all-http-to-https-in-iis)

    Login or Signup to reply.
  3. I have gone through this issue and i noticed other one answer given in this question is not working so i fixed this issue and would like to share what’s working for me. Please find below rewrite rule that is working to remove double slashes from url. we need to put it in web.config.

    <rewrite>
      <rules>
        <rule name="RemoveDoubleSlash" patternSyntax="ECMAScript" stopProcessing="true">
            <match url="(.*)" />
            <action type="Redirect" url="{C:1}/{C:2}" />
            <conditions>
                <add input="{UNENCODED_URL}" pattern="(.*)//(.*)" />
            </conditions>
        </rule>
      </rules>
    </rewrite>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search