Search code examples
url-rewritingurl-mapping

Change URL in address bar


I want to change my urls in my website. I read some articles and now, i know how to rewrite urls like this :

Users type in address bar => www.example.com/Q1
and loaded page is => www.example.com/dir1/cat.aspx?id=Q1

But i want this :

Users type in address bar => www.example.com/dir1/cat.aspx?id=Q1
and browsers show in address bar => www.example.com/othername/Q1

is there any way for this?


this is part of my webconfig about rewrite :

<system.webServer>
    <rewrite>
      <rules>
        <rule name="Rewrite page to aspx" stopProcessing="true">
          <match url="^([a-z0-9/]+)$" ignoreCase="false" />
          <action type="Rewrite" url="{R:1}.aspx" />
        </rule>
      </rules>
      <rule name="Rewrite item ID" stopProcessing="true">
        <match url="^items/([0-9]+)$" ignoreCase="false"/>
        <action type="Rewrite" url="items.aspx?id={R:1}"/>
      </rule>
      <rule name="Redirect to clean URL" stopProcessing="true">
        <match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/>
        <action type="Redirect" url="{R:1}"/>
      </rule>
    </rewrite>


Solution

  • I could not find my real answer. but i found a simple way to rewrite my urls without any settings and modules that solved my problem partly :

    you can rewrite in global.asax :

        void Application_BeginRequest(Object sender, EventArgs e)
        {
            String strCurrentPath;
            String strCustomPath;
            strCurrentPath = Request.Path;
            if ( strCurrentPath.EndsWith("/home/"))
            {
                strCustomPath = strCurrentPath.Replace("/home/", "/presentation/default.aspx");
                Context.RewritePath(strCustomPath);
                return;
            }
            else
            {
                if (strCurrentPath.Contains("/dir1/"))
                {
                    strCustomPath = strCurrentPath.Replace("/dir1/", "/othername/cat.aspx?cid=");
                    Context.RewritePath(strCustomPath);
                    return;
                }
            }
         }