1

I would like to rewrite URL with query string in ASP.NET. The URL looks like as following:

http://localhost:51392/productdetails.aspx?id=zdpMPZVXkDtjw92Crx7eew==

I would like to rewrite the url into something like this:

http://localhost:51392/Details/zdpMPZVXkDtjw92Crx7eew==

I have tried using the following method to rewrite the url (something like this):

 <urlMappings>
      <add url="~/Shop"   mappedUrl="~/shop.aspx"/>
</urlMappings>

But I'm not really sure how to properly map the URL with query string?? What is the easiest, or other method that can be used to achieve this??

Thanks!

P.S. guys this is how I'm trying to access the rewriten URL:

<a href='<%# "/productdetails.aspx?id=" + RL_DAL.RijndaelHelper.AES.Encrypt(Eval("ProductID").ToString(),"key_to_enrypt")  %>'><%# Eval("ProductName") %>
5
  • 1
    Possible duplicate of Url Rewriting in asp.net but maintaining the original url Commented Feb 4, 2016 at 22:21
  • If I am not mistaken you can use ServerVariables HTTP_X_ORIGINAL_URL to the query string part from your code. Commented Feb 4, 2016 at 22:22
  • Can you give me an example of how that would look like please ? :) Commented Feb 4, 2016 at 22:22
  • Check on the possible duplicate. I think you question was already answered Commented Feb 4, 2016 at 22:23
  • Still don't get it, where am I supposed to use this code? On the page load of the rewritten page I'm trying to access or? Commented Feb 4, 2016 at 22:25

3 Answers 3

6

In asp.net, hosted on an IIS platform -

  1. Install the URL Rewrite module into IIS
  2. Add this into the web.config (system.webServer) section of the site

    <rewrite>
        <rules>
            <rule name="rewrite" stopProcessing="true">
              <match url="productdetails.aspx" />
              <conditions>
                <add input="{QUERY_STRING}" pattern="id=([a-zA-Z0-9=]+)" />
              </conditions>
              <action type="Rewrite" url="/Details/{C:1}" appendQueryString="false" />
            </rule>
        </rules>
    </rewrite>
    

Then every request to /productdetails.aspx?id=zdpMPZVXkDtjw92Crx7eew== will be rewritten to http://localhost:51392/Details/zdpMPZVXkDtjw92Crx7eew==. But rewritten doesn't mean it will change for the user in their browser, just the destination on the server.

However, if you want to redirect it, change the rule to:

<rule name="redirect" stopProcessing="true">
  <match url="productdetails.aspx" />
  <conditions>
    <add input="{QUERY_STRING}" pattern="id=([a-zA-Z0-9=]+)" />
  </conditions>
  <action type="Redirect" url="/Details/{C:1}" redirectType="Permanent" appendQueryString="false" />
</rule>

This will do a permenent 301 redirect, to do a temporary 307 redirect change Permanent to Temporary on the action type.

See Redirect vs Rewrite for more information on the difference between the two.


With the redirect rule I gave:

The user enters /productdetails.aspx?id=zdpMPZVXkDtjw92Crx7eew== they are redirected to /Details/zdpMPZVXkDtjw92Crx7eew== and that URL must be capable of processing the request. This scencrio is typically used when moving a site to a new platform / URL structure.

To do the opposite, with the user entering /Details/zdpMPZVXkDtjw92Crx7eew== and the site processing the request on /productdetails.aspx?id=zdpMPZVXkDtjw92Crx7eew== use:

<rule name="rewrite">
    <match url="Details/([a-zA-Z0-9=]+)" />
    <action type="Rewrite" url="/productdetails.aspx?id={R:1}" />
</rule>

(Example on GitHub)

Sign up to request clarification or add additional context in comments.

9 Comments

"But rewritten doesn't mean it will change for the user in their browser, just the destination on the server." But the whole point of my question was to rewrite the URL in users browser :/
Then you want to redirect it :-) Use the second rule.
Added a link for rewrite vs redirect, check out the table towards the top of that blog post which outlines the difference between the two.
Okay, im using the second rule, but its not rewriting the URL at all... And When I click the link now its showing me: HTTP Error 404.0 - Not Found
try now, I missed a slash in the destination url
|
0

in global.asax

void RegisterRoutes(RouteCollection routes)
    {
         routes.MapPageRoute("Products","Products/{id}/{productname}",
                           "~/productdetails.aspx");
}

in products.aspx

<ahref='<%#string.Format("/Products/{0}/{1}",Eval("id"),Clean.CleanUrl(Eval("ProductName").ToString())) %>'><%# Eval("ProductName") %></a>

in productsdetails.aspx.cs

if (!IsPostBack)
        {
            if (RouteData.Values["id"] != null)
            {
               loadproduct();

            }
}

void loadproduct()
{
 select .............  where id= RouteData.Values["id"].ToString();
}

This is Clean Class

public static class Clean
    {
        public static string CleanURL(this string kelime)
        {
            if (kelime == "" || kelime == null) { return ""; }
            kelime = kelime.Replace("ş", "s");
            kelime = kelime.Replace("Ş", "S");
            kelime = kelime.Replace(".", "");
            kelime = kelime.Replace(":", "");
            kelime = kelime.Replace(";", "");
            kelime = kelime.Replace(",", "");
            kelime = kelime.Replace(" ", "-");
            kelime = kelime.Replace("!", "");
            kelime = kelime.Replace("(", "");
            kelime = kelime.Replace(")", "");
            kelime = kelime.Replace("'", "");
            kelime = kelime.Replace("ğ", "g");
            kelime = kelime.Replace("Ğ", "G");
            kelime = kelime.Replace("ı", "i");
            kelime = kelime.Replace("I", "i");
            kelime = kelime.Replace("ç", "c");
            kelime = kelime.Replace("ç", "C");
            kelime = kelime.Replace("ö", "o");
            kelime = kelime.Replace("Ö", "O");
            kelime = kelime.Replace("ü", "u");
            kelime = kelime.Replace("Ü", "U");
            kelime = kelime.Replace("`", "");
            kelime = kelime.Replace("=", "");
            kelime = kelime.Replace("&", "");
            kelime = kelime.Replace("%", "");
            kelime = kelime.Replace("#", "");
            kelime = kelime.Replace("<", "");
            kelime = kelime.Replace(">", "");
            kelime = kelime.Replace("*", "");
            kelime = kelime.Replace("?", "");
            kelime = kelime.Replace("+", "-");
            kelime = kelime.Replace("\"", "-");
            kelime = kelime.Replace("»", "-");
            kelime = kelime.Replace("|", "-");
            kelime = kelime.Replace("^", "");
            return kelime;
        }
    }

1 Comment

It rewrites the URL, however, the answer doesn't includes how to rewrite the URL with query string??
0

In global.asax.cs file, try to add code in this way.

    void Application_BeginRequest(object sender, EventArgs e) {

    string fullpath = Request.Url.ToString();

    if (fullpath.Contains("/Details/zdpMPZVXkDtjw92Crx7eew==")) {
        Context.RewritePath("/productdetails.aspx?id=zdpMPZVXkDtjw92Crx7eew==");
    }
   }

And then in the Productdetails.aspx.cs file, you may try to read the Id.

Label1.Text = Request.QueryString["id"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.