locked
URL Redirection RRS feed

  • Question

  • User-614943948 posted

    I found this source code, for hiding the name and extension for the ASPX page. 

    <?xml version="1.0"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="default.aspx Redirect" stopProcessing="true">
                        <match url="^(.*\/)*default\.aspx$" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" />
                        </conditions>
                        <action type="Redirect" url="{R:1}" redirectType="Permanent"/>
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>

    But this only works with the default page.  I want to hide all the pages within the application. How can I accomplish this

    Thursday, February 4, 2021 12:13 PM

Answers

  • User-939850651 posted

    Hi maverick786us,

    is there a way to have both the modes on?

    This cannot be implement.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2021 9:19 AM

All replies

  • User571301025 posted

    Hi,

    Hope if you adjust the regex in the "match" tag of "url" property may work. Try as below, which will work for aspx file extension:

    <?xml version="1.0"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="default.aspx Redirect" stopProcessing="true">
                        <match url="^.*\.(aspx)$" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{REQUEST_METHOD}" negate="true" pattern="^POST$" />
                        </conditions>
                        <action type="Redirect" url="{R:1}" redirectType="Permanent"/>
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>

    For more info, refer the below stack overflow discussion:

    https://stackoverflow.com/questions/38782682/iis-redirect-urls-which-are-not-ending-with-aspx-to-some-page

    Hope this helps!!!

    Thursday, February 4, 2021 7:23 PM
  • User-1545767719 posted

    If your ASP.NET version is 4.5 or above I suggest you to use the FriendlyUrlSettings Class:

    https://docs.microsoft.com/en-us/previous-versions/aspnet/jj879443(v=vs.111)

    It will be included in the project produced by the Visual Studio template and enabled by default as follows:

    App_Start\RouteConfig.cs

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.Routing;
    using Microsoft.AspNet.FriendlyUrls;
    
    namespace WebApplication2
    {
        public static class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                var settings = new FriendlyUrlSettings();
                settings.AutoRedirectMode = RedirectMode.Permanent;
                routes.EnableFriendlyUrls(settings);
            }
        }
    }

    Global.asax.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Optimization;
    using System.Web.Routing;
    using System.Web.Security;
    using System.Web.SessionState;
    
    namespace WebApplication2
    {
        public class Global : HttpApplication
        {
            void Application_Start(object sender, EventArgs e)
            {
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }
        }
    }

    Friday, February 5, 2021 1:01 AM
  • User-614943948 posted

    Yes I am using .NET 4.5 but the project was created in an older version of .NET and it doesn't have what that RouteConfig.cs 

    Friday, February 5, 2021 11:28 AM
  • User-614943948 posted

    I used this code for the login page which is index.aspx it worked. But when I tried to login, it didn't take me the redirected webpage, but generated an IIS Error 

    HTTP Error 404.0 - Not Found
    The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

    Friday, February 5, 2021 11:43 AM
  • User-939850651 posted

    Hi maverick786us,

    I used this code for the login page which is index.aspx it worked. But when I tried to login, it didn't take me the redirected webpage, but generated an IIS Error 

    HTTP Error 404.0 - Not Found
    The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

    This error suggests that the file you are looking is not present on the server or web.config file is not configured properly. 

    For more configuration guidelines, please refer to this document:

    https://docs.microsoft.com/en-us/previous-versions/ff400235(v=vs.140)

    Best regards,

    Xudong Peng

    Wednesday, February 10, 2021 2:48 AM
  • User-614943948 posted

    I created a RouteConfig file in App_Start folder 

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings);
        }
    }

    Using this code I was able to hide the extension name. Now I added these rules of code in my web.config file

        <!--<customErrors mode="Off"/>-->
        <customErrors mode="RemoteOnly" defaultRedirect="~/CustomError.aspx">
          <error statusCode="400" redirect="~/CustomError.aspx" />
          <error statusCode="401" redirect="~/CustomError.aspx" />
          <error statusCode="403" redirect="~/CustomError.aspx" />
          <error statusCode="404" redirect="~/CustomError.aspx" />
          <error statusCode="500" redirect="~/CustomError.aspx" />
        </customErrors>

    But when its not leading me to the custom error page. None of these conditions are getting fulfilled

    Wednesday, February 10, 2021 8:37 AM
  • User-939850651 posted

    Hi maverick786us,

    maverick786us

    But when its not leading me to the custom error page. None of these conditions are getting fulfilled

    Are you testing locally?

    If this is the case, you need to modify mode="On" in customErrors instead of RemoteOnly.

    <customErrors mode="On" defaultRedirect="~/CustomError.aspx">
          <error statusCode="400" redirect="~/CustomError.aspx" />
          <error statusCode="401" redirect="~/CustomError.aspx" />
          <error statusCode="403" redirect="~/CustomError.aspx" />
          <error statusCode="404" redirect="~/CustomError.aspx" />
          <error statusCode="500" redirect="~/CustomError.aspx" />
        </customErrors>

    For more details, you could read this doucment.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.configuration.customerrorsmode?view=netframework-4.8

    Hope this can help.

    Best regards,

    Xudong Peng

    Tuesday, February 16, 2021 8:03 AM
  • User399856419 posted
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings);
        }
    }

    Using this code I was able to hide the extension name. Now I added these rules of code in my web.config file

        <!--<customErrors mode="Off"/>-->
        <customErrors mode="RemoteOnly" defaultRedirect="~/CustomError.aspx">
          <error statusCode="400" redirect="~/CustomError.aspx" />
          <error statusCode="401" redirect="~/CustomError.aspx" />
          <error statusCode="403" redirect="~/CustomError.aspx" />
          <error statusCode="404" redirect="~/CustomError.aspx" />
          <error statusCode="500" redirect="~/CustomError.aspx" />
        </customErrors>


    great sir
    Wednesday, February 17, 2021 4:53 PM
  • User-614943948 posted

    Thanks I will try. So this way it will work locally as well as through internet?

    Thursday, February 18, 2021 8:29 AM
  • User-939850651 posted

    Hi maverick786us,

    Have you read the document I mentioned above?

    In RemoteOnly mode, it enables custom errors on remote clients only. Custom errors are shown only to remote clients and ASP.NET errors are shown to the local host. Therefore, under local testing, it does not display custom errors.

    Regarding the difference between these two modes, you can also refer to this case:

    Difference between customErrors mode="On" and "RemoteOnly" in web.config

    Best regards,

    Xudong Peng

    Friday, February 19, 2021 7:40 AM
  • User-614943948 posted

    Thanks, is there a way to have both the modes on?

    Friday, February 19, 2021 9:21 AM
  • User-939850651 posted

    Hi maverick786us,

    is there a way to have both the modes on?

    This cannot be implement.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 23, 2021 9:19 AM