Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

rewrite every page to www.

Posted on 2011-03-09
10
Medium Priority
?
317 Views
Last Modified: 2013-12-17
Hi.

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

http://domain.com -> http://www.domain.com
http://domain.com/test.aspx -> http://www.domain.com/test.aspx
http://domain.com/page/test.aspx?ID=3 -> http://www.domain.com/page/test.aspx?ID=3

I have implemented http://urlrewriter.net/ in my project, dosen´t know if it can help.
0
Comment
Question by:bongii
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 4

Expert Comment

by:WhiteSeed
ID: 35080766
0
 
LVL 4

Accepted Solution

by:
Loftbug earned 2000 total points
ID: 35080879
Hi Bongii,

We use this method in our client solutions. It works well.
Put this code in your global.asax file.

Best,

 L



// this event is found in global.asax
protected void Application_BeginRequest(Object sender, EventArgs e)
{
   RedirectToWWW();
}

// place this method also in your global.asax page
private static void RedirectToWWW()
{

      // note: uncomment if you need to prevent adding www. when testing
      // if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("localhost"))
      // return;

			// Redirect to www.clienturl.com if the user types in clienturl.com.
			if (HttpContext.Current.Request.Url != null 
				&& !String.IsNullOrEmpty(HttpContext.Current.Request.Url.Host)				
				&& !HttpContext.Current.Request.Url.Host.ToLower().StartsWith("www"))
			{        
				HttpContext.Current.Response.Redirect("http://www." + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.Url.AbsolutePath);
			}
}

Open in new window

0
 

Author Comment

by:bongii
ID: 35081787
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
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 4

Expert Comment

by:WhiteSeed
ID: 35081871
i had uses that on my website, but it use .htaccess

http://teqsnacks.com/2007/01/17/automatically-redirecting-to-the-www-prefix/

Open in new window

0
 
LVL 19

Expert Comment

by:Bardobrave
ID: 35081891
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 %>">
0
 
LVL 4

Expert Comment

by:Loftbug
ID: 35082212
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 www.mydomain.com


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

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

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.

cheers,

 L
0
 

Author Comment

by:bongii
ID: 35082593
Hi loftbug.
I will try right now :)

I just read about the routing class, but it think it only supports .net 4
0
 

Author Comment

by:bongii
ID: 35083013
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" />
result:
www.domain.com/video/video1.aspx

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

Can you help?
0
 
LVL 4

Expert Comment

by:Loftbug
ID: 35084528
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 UrlRewriter.net .

>> 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 --> www.domain.com/video1/
    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 --> http://stackoverflow.com/questions/459203/how-do-i-configure-extensionless-urls-with-the-visual-web-development-server


I hope this makes sense and helps.

Best,

 Loftbug










0
 

Author Comment

by:bongii
ID: 35084789
Thank you very muck for your help. I will check the articles tomorrow :)
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question