Page Redirect

Discussion in 'General troubleshooting' started by MWNWT, Nov 19, 2014.

  1. Is there a way to set up a custom error rule that will route to the proper page without its .aspx extension? We had some documentation go out with the incorrect url (its missing the .aspx extension), so I wanted to set up a way for users to go to the site without the .aspx extension and still reach the proper page.

    Thank you very much.
     
  2. martino

    martino DiscountASP.NET Staff

    That would consist of a 404 error page severed by ASP.NET Error Handling. But I don't think setting up a 404 error page is what you're looking for.

    I believe what you're looking for is a URL Rewrite rule to help you solve this issue. It happens I know but lucky URL Rewrite is very powerful and it can help in this situation.

    I been playing around with URL Rewrite these last few days and I think I might have found a solution for you. With that you need to understand that I don't know the name of the page your message contains but in my example below. I made up situation where I forgot the .aspx extension like yourself.

    Lets just say my site was MySiteMartin.com and I sent an email with a link called http://mysitemartin.com/mypage

    However, my link I forgot the .aspx extension. So when they go to that link they are going to get a server's 404 error message. Why? Because the server is looking for a directory and it doesn't know your looking for a page called mypage.aspx

    The following URL Rewrite code should fix this. Basically it's saying. If the URL contains mypage rewrite to have the .aspx extension. But it also doesn't include the aspx extension in the URL in the web browser but still brings up the page I want.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rewriteMaps>
                    <rewriteMap name="/mypage">
                    </rewriteMap>
                </rewriteMaps>
                <rules>
                    <rule name="Name of this Rule">
                        <match url="^mypage$" />
                        <action type="Rewrite" url="{R:0}.aspx" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
    
    Take the code and replace mypage with the real name of your page. Remember the code will only work for the exact page name.

    It also hasn't been full tested so make sure you make a backup of what ever changes you make in your web.config file. That way if something goes bad you can always revery back to your backup web.config file.
     
    mjp likes this.

Share This Page