Trido
Trido

Reputation: 545

Second trailing slash in redirected URL

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>

Upvotes: 1

Views: 3978

Answers (3)

Sigja
Sigja

Reputation: 93

Just remove the forward 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)

Upvotes: 5

Heemanshu Bhalla
Heemanshu Bhalla

Reputation: 3765

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>

Upvotes: 0

Ding Peng
Ding Peng

Reputation: 3974

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>

Upvotes: 1

Related Questions