I'm guessing that you want to do this because you want your users to see pretty looking URLs. The only way to get the client to "change" the URL in its address bar is to send it to a new location - i.e. you need to redirect them.
Are the query string parameters going to affect the output of your page? If so, you'll have to look at how to maintain state between requests (session variables, cookies, etc.) because your query string parameters will be lost as soon as you redirect to a page without them.
There are a few ways you can do this globally (in order of preference):
- If you have direct control over your server environment then a configurable server module like ISAPI_ReWrite or IIS 7.0 URL Rewrite Module is a great approach.
- A custom
IHttpModule
is a nice, reusable roll-your-own approach.
- You can also do this in the
global.asax
as you suggest
You should only use the 301
response code if the resource has indeed moved permanently. Again, this depends on whether your application needs to use the query string parameters. If you use a permanent redirect a browser (that respects the 301
response code) will skip loading a URL like *.../default.aspx?utm_source=twitter&utm_medium=social-media* and load .../default.aspx - you'll never even know about the query string parameters.
Finally, you can use POST
method requests. This gives you clean URLs and lets you pass parameters in, but will only work with <form>
elements or requests you create using JavaScript.