Link to home
Start Free TrialLog in
Avatar of Jesper Christensen
Jesper Christensen

asked on

rewrite every page to www.


Is it possible to add a script in web.config that redirects every page to www.
F.x: -> -> ->

I have implemented in my project, dosen´t know if it can help.
Avatar of WhiteSeed
Flag of Italy image

Avatar of Loftbug

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jesper Christensen
Jesper Christensen


White seed: It dosen´t work.
The element <rewrite> is not a part of  <system.webServer>

Loftbug: I am using a rewrite engine, so the script can´t find the rewrited urls
i had uses that on my website, but it use .htaccess

Open in new window

A different approach:

You can use a refresh into a meta tag in your pages. This way you can have a partial view in your master page where you injects the meta refresh with the current url converted to www.

<meta http-equiv="refresh" content="0;url=<%= "http://www" + yourCurrentUrlRequested %>">
That makes sense. Your UrlRewriter matching rule may be checking for domains with no www. prefix/subdomain.
Change it to ignore domain level matching or duplicate your rule set for

This is what I did for one of my UrlRewriter solutions long ago.

For example:
<if header="HTTP_HOST" match="^www\.([^\.]+)\.com$">
 <!-- copy rewrite actions here, ensure actions dont depend on domain, only path -->

Use this together with the global.asax methods I posted before and see if it works.

By the way, you may want to look into Asp.Net routing, I switched over to that last year. Its clean and easily debug-able.


Hi loftbug.
I will try right now :)

I just read about the routing class, but it think it only supports .net 4
Loftbug your trick worked with the urlrewriter :) Thank you very much.

Do you have expirence with the url rewrite rules?
I have a little problem, which I can´t solve.

My rule:
<rewrite url="~/category/(.+)" to="~/video.aspx?URL=$1" />

I would like to change the rule so the url replace .aspx with "/" and "category" with ""
So the result would be:

Can you help?
Hi Bongii,

Aah thats good to hear.

You can setup routing with .net 3.5, I have it running there on many of our client applications. (many shared hosting servers also dont support .net 4 so we use .net 3.5 for our solutions.)
Here is an article that can get you started. There is a bit of a learning curve but I found it afterwards easier to use and maintain than .

>> Do you have expirence with the url rewrite rules?

I worked on it quite a few years back. Here is what I can remember.

To run extensionless url's:

1. All your page clickable urls must be friendly and in the format -->
    Friendly urls start with coding them into your aspx page in their friendly format so users clicking them make a friendly url request.
    Then the ReWriter engine must identify and route the friendly url request to the correct underlying physical aspx page.

2. Next step, your web server needs to be configured to support executing extensionless urls.
    This is done differently on IIS6 (little harder) and IIS7 (easy).

This article explains a bit more on using urlrewriter -->

I hope this makes sense and helps.



Thank you very muck for your help. I will check the articles tomorrow :)