URL Rewriting not working for my .net Core application [Answered]RSS

6 replies

Last post Sep 12, 2019 06:04 PM by shuja075

  • URL Rewriting not working for my .net Core application

    Sep 11, 2019 04:14 AM|shuja075|LINK

    I have built a MVC .Net Core Application. I have hosted it on the server. (With concept one hosting multiple domains)

    Folder structure is as below

    MasterSite (Also MVC Site)
            ChildSite1
            ChildSite2
            Movingtrend

    I have very basic knowledge of IIS related settings. I followed a blog guide to use URL rewriting. everything is working fine. except i don't want to show the Sub-folder directory in which the child site is hosted.

    because it's causing an issues with my login system. I would really appreciate if anyone write exact Inbound and outbound rule for this situation.

    my current URL re-write both Inbound and outbound rule

    <rewrite>
          <rules>
            <rule name="Movingtrend Rule" enabled="true" stopProcessing="true">
              <match url=".*" />
              <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_HOST}" pattern="^(www\.)?Movingtrend\.com" />
                <add input="{PATH_INFO}" pattern="^/Movingtrend($|/)" negate="true" />
              </conditions>
              <action type="Rewrite" url="/Movingtrend/{R:0}" />
            </rule>
          </rules>
          <outboundRules>
            <rule name="Outgoing - URL paths" enabled="true">
              <match filterByTags="A" pattern="^(?:Movingtrend|(.*//[_a-zA-Z0-9-\.]*)?/Movingtrend)(.*)" />
              <action type="Rewrite" value="{R:1}{R:2}" />
            </rule>
            <rule name="response_location URL">
              <match serverVariable="RESPONSE_LOCATION" pattern="^(?:Movingtrend|(.*//[_a-zA-Z0-9-\.]*)?/Movingtrend)(.*)" />
              <action type="Rewrite" value="{R:1}{R:2}" />
            </rule>
            <rule name="response_location querystring">
              <match serverVariable="RESPONSE_LOCATION" pattern="(.*)%2fMovingtrend(.*)" />
              <action type="Rewrite" value="{R:1}{R:2}" />
            </rule>
          </outboundRules>
    
        </rewrite>

    You may visit the actual site to get the idea. 

    http://movingtrend.com/ 

    http://movingtrend.com/Movingtrend

    I want to remove that Extra "/Movingtrend" Portion.

  • Re: URL Rewriting not working for my .net Core application

    Sep 11, 2019 09:10 AM|Yuk Ding|LINK

    Hi shuaja075,

    May I know how do you want to handle the subfolder/Movingtrend ? You want IIS ignore this folder or you mean when user access http://movingtrend.com/Movingtrend it will be redirected http://movingtrend.com/Then IIS rewrite the content back to

    http://movingtrend.com/Movingtrend?

    Best Regards,

    Jokies Ding

    Yuk Ding

    MSDN Community Support
    Please remember to "Mark as Answer" the responses that resolved your issue.
  • Re: URL Rewriting not working for my .net Core application

    Sep 11, 2019 09:31 AM|shuja075|LINK

    Thanks for Replying. 

    Actually I want to hide that /Movingtrend From my URL. The reason behind is this.

    When i try to login on website using the below credentials. 

    User logged in to the system successfully but i am not able to see him logged-in using  below link.

    http://movingtrend.com/Home/Index

    http://movingtrend.com

    instead i can see him logged-in via 

    http://movingtrend.com/movingtrend/Home/Index

    Which is not correct and that causes the issue to my all URL's of website.

    The above mentioned (Re-write Rule for Both Inbound and outbound ) is currently working. Please check. Your help shall highly be appreciated. Thanks in Advance.

  • Re: URL Rewriting not working for my .net Core application

    Sep 12, 2019 07:16 AM|Yuk Ding|LINK

    Hi shuaja075,

    I tried to monitor your link. And I also notice that when I access /home/index, the website is not setting Set-Cookie: .AspNetCore.Identity.Application and  .AspNetCore.Antiforgery in the request. It was only displayed in response.  When I access movingtrend/Home/Index directly, I will see cookies being delivered via request not response. So you are using cookie authentication right?

    I think the problem is your root folder movingtrend/Home/Index and http://movingtrend.com/Home/Index belong to different sub-application. The problem should not be caused by IIS URL rewrite.

    Maybe you need to set something like SSO for your root folder to collect aspnetcore.identity.application cookie.

    Besides, I think you should remove password and username from your post for security reason.

    Best Regards,

    Jokies Ding

    Yuk Ding

    MSDN Community Support
    Please remember to "Mark as Answer" the responses that resolved your issue.
  • Re: URL Rewriting not working for my .net Core application

    Sep 12, 2019 07:34 AM|shuja075|LINK

    Once again thanks for replying. 

    Yes i am using Authentication via Identity. It is working locally on my machine even after publishing. But when i publish it on share hosting with a concept (One Hosting multiple sites) It starts giving above exception. 

    Also ValidateAntiForgeryToken was intentionally disabled on my account controller. But now when i enabled  it on my account  controller login and Registration form it starts giving error. What could be the reason? I am so confused. 

  • Re: URL Rewriting not working for my .net Core application

    Sep 12, 2019 08:20 AM|Yuk Ding|LINK

    Hi shuja075,

    I think the problem come from the concept One Hosting multiple sites. It seems that cookie authentication is not covering the whole website. Its just covering /movingtrend/. IIS does add identity cookie to request header. Your application control this. I think you need to make some change in architecture and code to make cookie authentication cover the URL /home/index. Then no matter you access  /movingtrend/home/index or /home/index. Identity cookie will be checked all the time.

    Best Regards,

    Jokies Ding

    Yuk Ding

    MSDN Community Support
    Please remember to "Mark as Answer" the responses that resolved your issue.
  • Re: URL Rewriting not working for my .net Core application

    Sep 12, 2019 06:04 PM|shuja075|LINK

    Thank you Very much. Yeah You were right. Issue resolved after covering the whole website with cookie authentication.

    In addtion can you guide me for the Inbound/ outbound rules for IIS to hide/remove that /Movingtrend parth From the URL?

    Because my some of the URL's are like this.

    http://movingtrend.com/Movingtrend

    http://movingtrend.com/Movingtrend/Manage/Account

    http://movingtrend.com/Movingtrend/Archive/2018/8/google-prepared-display-notch-guidelines-for-device-manufacturers-and-developers/

    I want them as below.

    http://movingtrend.com/

    http://movingtrend.com/Manage/Account

    http://movingtrend.com/Archive/2018/8/google-prepared-display-notch-guidelines-for-device-manufacturers-and-developers/

    Even though URL's are working without /Movingtrend.

    But i want IIS to remove that additional part if it comes to my any URL.

    Once again your kind help is much appreciated. Thank you very much.