Solved

url rewrite returns 404

Posted on 2007-12-07
35
2,086 Views
Last Modified: 2008-02-21
hi Experts-Exchange,

I have  a problem with my rewrite working on my live server.  
I have the following rewrite rule which works locally for me but I have uploaded the global.asax file with this code in and it returns 404 for this url:

         http://aaa.bbb.ccc/Category/Test/


I dont undestand why I get a 404 when the I am using Response.End() within the global file?

If I replace the code within the code snipper field below with the following I still get a 404 for the url above?

void Application_BeginRequest(object sender, EventArgs e)
{
    Response.End();
}

With my little knowledge its as though the url  http://aaa.bbb.ccc/Category/Test/ is actually looking for the page  http://aaa.bbb.ccc/Category/Test/Default.aspx which obvously doesnt exist!

Please advise how to resolve this issue so that I can get the Context.RewritePath rule working.

Many thanks,
To place the files on the live server I have simply uploaded each default.aspx, default.aspx.cs file together.  I have not published the site.  Just mentioning that incase it makes a difference.  
All other normal pages (i.e. without a rewrite) work fine.
void Application_BeginRequest(object sender, EventArgs e)

    {

        string url = "";

        url = Request.Path.Substring(0, Request.Path.LastIndexOf("/") + 1);

        Match m = Regex.Match(url, "^.*/Category/([^/]*)/?([^/]*)/?$");

 

        if ((m.Success) && (m.Groups.Count == 3))

        {

            if (m.Groups[1].Value != "")

            {

                Response.Write("here");

                Response.End();

                //Context.RewritePath("/Category/Default.aspx?name=" + m.Groups[1].Value);

            }

        }

Open in new window

0
Comment
Question by:jimbona27
  • 20
  • 13
35 Comments
 
LVL 16

Expert Comment

by:gnoon
ID: 20427086
AFAIK, Response.End() only be used to debug program, and will pause from redering response to client.

I think, the 404 error is thrown because user requests to default.aspx. It never be reached but stuck at global.asax. So, the IIS determines that the default.aspx is not found, and then throws the error to client.

Instead of response.end(), better redirect to another page.
0
 

Author Comment

by:jimbona27
ID: 20427119
ok, i've made this change (see code snippet),

I've also created a test.aspx file at the root.

the url I use for this example is:
http://aaa.bbb.ccc/Test/Hello-World/

the result produces a 404 message..??

Any ideas?


    void Application_BeginRequest(object sender, EventArgs e)

    {

        string url = "";

        url = Request.Path.Substring(0, Request.Path.LastIndexOf("/") + 1);

        

        Match mm = Regex.Match(url, "^.*/Test/([^/]*)/?([^/]*)/?$");

        if ((mm.Success) && (mm.Groups.Count == 3))

        {

            if (mm.Groups[1].Value != "")

            {

                Response.Redirect("/test.aspx");

            }

        }

    }

Open in new window

0
 

Author Comment

by:jimbona27
ID: 20427790
is there a way I can get around iis picking up that the page does not exist?
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20428358
By default, the startup page of Web Application is set to default.htm or default.asp. You can add another file name into the list. IIS will search over the path for a file matched in the list prior.

To set it on server, goto Control Panel -> Administrative -> Internet Information Services
Open property on Default Web Site -> Documents tab -> Add default.aspx to the list and make it as the first item.
0
 

Author Comment

by:jimbona27
ID: 20434287
i know this can be done but I cant check whether this is set or not right now but surely the fact I can type in
www.mydomain.com to get the www.domain/default.aspx page indicates this is already set?

i'll be able to try on monday but are you saying if i place default.aspx in the document tab within iis my rewrite rule will work?

0
 
LVL 16

Expert Comment

by:gnoon
ID: 20436183
>you saying if i place default.aspx in the document tab within iis my rewrite rule will work?
No, it's not solving url rewrite. Actually, i just understand your problem (which need to read in code algorithm).

My understand:
  You typed => http://aaa.bbb.ccc/Category/Test/
  You expect => http://aaa.bbb.ccc/Category/Default.aspx?name=Test

Is it successfully loaded if you just enter
                          http://aaa.bbb.ccc/Category/Default.aspx?name=Test
in a browser?

According to the original code above, I think you need to improve the regex expression to prevent endless loop of request. Please try with this code below.

Also, you can use another sulotions at here http://www.na-vigator.com/index.php?option=com_content&task=view&id=164&Itemid=37
void Application_BeginRequest(object sender, EventArgs e)

{

    string url = Request.Path;

    Match m = Regex.Match(url, "^.*/Category/([^/]*)/?(.*)$", RegexOptions.IgnoreCase);
 

    if ((m.Success) && (m.Groups.Count > 1))

    {

        if (m.Groups[1].Value != string.Empty &&

            !m.Groups[1].Value.ToLower().StartsWith("default.aspx?name="))

        {

            Context.RewritePath("/Category/Default.aspx?name=" + m.Groups[1].Value);

        }

    }

}

Open in new window

0
 

Author Comment

by:jimbona27
ID: 20442886
works locally but not on live server?
i'll have to have a look at iis,
if thats possible...
0
 

Author Comment

by:jimbona27
ID: 20448429
I have changed the code to this now for this url: http://aaa.bbb.ccc/Category/Test/ and it fails with "The page cannot be found".
it works locally.

    void Application_BeginRequest(object sender, EventArgs e)
    {
        string fullOrigionalpath = Request.Url.ToString();
        Response.Write(fullOrigionalpath);
        if (fullOrigionalpath.Contains("Category"))
        {
            Context.RewritePath("/Default.aspx?Name=Test");
        }
    }
0
 

Author Comment

by:jimbona27
ID: 20448474
iis i've been has supported url rewriting before..
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20449475
OK. After searched through the net, I found that many people faced the same problem of you.

Here the solutions

- Use
   Context.RewritePath("/Default.aspx", "", "Name=Test"); // it requires actual file path
or
   Response.Redirect("/Default.aspx?Name=Test", true); // redirect and terminate from current execution

@http://www.codeproject.com/KB/aspnet/dnn2url_rewrite.aspx

- If there is querystring, just call RewritePath twice

   string url = Request.Url.ToString();
   if(Context.Request.QueryString.Count = 0 && url.IndexOf("?") != -1)
   {
      Context.RewritePath(url);
      Context.RewritePath(url);
   }
   else
   {
      Context.RewritePath(url);
   }

@http://www.dotnet247.com/247reference/msgs/45/226746.aspx
0
 

Author Comment

by:jimbona27
ID: 20470862
using

   Context.RewritePath("/Default.aspx", "", "Name=" + m.Groups[1].Value);

i get "The page cannot be found"??

0
 
LVL 16

Expert Comment

by:gnoon
ID: 20470930
It's maybe problem of relative path. What's if

Context.RewritePath("/Category/Default.aspx", "", "Name=" + m.Groups[1].Value);
or
Context.RewritePath("Default.aspx", "", "Name=" + m.Groups[1].Value);

It's hard to say without see the code of Global.asax and Default.aspx.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20470998
Context.RewritePath("/Default.aspx", "", "Name=" + m.Groups[1].Value);

this means http://aaa.bbb.ccc/Default.aspx.  Is it there?
0
 

Author Comment

by:jimbona27
ID: 20471038
sorry, i've tried this

   Context.RewritePath("/Default.aspx", "", "Name=" + m.Groups[1].Value);

and it loads the homepage,

changing it to what I want, i.e.

   Context.RewritePath("/Category/Default.aspx", "", "Name=" + m.Groups[1].Value);

and it returns page cannot be displayed.
0
 

Author Comment

by:jimbona27
ID: 20477946
global file.


    void Application_BeginRequest(object sender, EventArgs e)

    {

        string url = "";

        url = Request.Path.Substring(0, Request.Path.LastIndexOf("/") + 1);

        

        Match m = Regex.Match(url, "^.*/Category/([^/]*)/?([^/]*)/?$");

        if ((m.Success) && (m.Groups.Count == 3))

        {

            if (m.Groups[1].Value != "")

            {

                Context.RewritePath("/Category/Default.aspx", "", "Name=" + m.Groups[1].Value);

            }

        }
 

    }

Open in new window

0
 

Author Comment

by:jimbona27
ID: 20477952
the /Category/Default.aspx simply is a hello world page to get this working..
anything else I provide?
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20482265
What's your application name?
Isn't it /<ApplicationName>/Category/Default.aspx?
Is your app under a virtual directory or under the home directory on IIS?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:jimbona27
ID: 20483732
i will find out as i've never actually seen the server, just an ftp folder to upload to.
0
 

Author Comment

by:jimbona27
ID: 20483760
its likely to be a virtual directory as its a shared hostage
0
 

Author Comment

by:jimbona27
ID: 20484107
i have made the following change although I still get the same result.

    Context.RewritePath("/nameofapp/Category/Default.aspx", "", "Name=" + m.Groups[1].Value);

0
 

Author Comment

by:jimbona27
ID: 20488786
any ideas?
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20490355
After you uploaded the app, just test for existence first.
1. Enter the URL in the browser's address, like this

http://aa.bb.cc/nameofapp/Category/Default.aspx       success?

2. You want to rewrite this URL

http://aa.bb.cc/nameofapp/Category/Test/   to this   http://aa.bb.cc/nameofapp/Category/Default.aspx?Name=Test

So, test if the second URL's already available as 1.
0
 

Author Comment

by:jimbona27
ID: 20490888
entered the following urls into the browser,

http://aa.bb.cc/nameofapp/Category/Default.aspx  // fails:

http://aaa.bbb.ccc/Category/Default.aspx  // display "Server Error in '/' Application.", id assume this is okay because there is no name to display within the querystring.

http://aaa.bbb.ccc/Category/?name=Test  // works

0
 

Author Comment

by:jimbona27
ID: 20492674
does that help?
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20497052
>does that help?
Yes, that does a lot.

Next, we need to make sure that the Application_BeginRequest is really working on that server by redirect user to another page. Like this

      void Application_BeginRequest(object sender, EventArgs e)
      {
            HttpContext httpContext = HttpContext.Current;
            httpContext.Response.Redirect("http://aaa.bbb.ccc/Category/?name=Test", true);
      }

also try this

      void Application_BeginRequest(object sender, EventArgs e)
      {
            HttpContext httpContext = HttpContext.Current;
            httpContext.RewritePath("http://aaa.bbb.ccc/Category/?name=Test");
            //httpContext.RewritePath("http://aaa.bbb.ccc/Category/", "", "name=Test");
      }

If all above are working, then just step into regular expression and use full qualified URL.
Hope this can help you make it work.
0
 

Author Comment

by:jimbona27
ID: 20498551
using this

      void Application_BeginRequest(object sender, EventArgs e)
      {
            HttpContext httpContext = HttpContext.Current;
            httpContext.Response.Redirect("http://aaa.bbb.ccc/Category/?name=Test", true);
      }

produces this url:
      http://www.aaa.bbb/Category/?name=Test

notice the presence of www after the redirect and the removed ccc?

this url produces server not found message.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20498626
Actually, Redirect does not go to the specify URL directly, but sends notification to the browser to change page to the specified URL. Some browsers, include IE, automatically add the www infront the domain name, if need.

If there is http:// infront the domain name, it shouldn't add www infront. But this case, I wonder why it does.
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20499141
>If there is http:// infront the domain name, it shouldn't add www infront.
I'm wrong here. It does.

Can you show the actual domain name of aaa.bbb.ccc?
0
 

Author Comment

by:jimbona27
ID: 20500526
go here,
http://www.brandperfumes.co.uk/devel/

(this link will transfer you to the actual domain)

the 3 links on the left panel are what we have been talking... notice im using /?name=xxx to test the pages although these links should support the seo friendly urls

the global file is as above so it should allow you to write /Category/xxx/ instead of the querystring.

let me know if you need more
0
 

Author Comment

by:jimbona27
ID: 20501516
any ideas? many thanks
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20504068
I think I found it.

The Global.asax's Application_BeginRequest will be processed only when the known-extension of ISAPI is requested.

http://area1.chunktestarea.com/Category/?name=Fashion ---- OK
http://area1.chunktestarea.com/Category/Fashion/ -------- Failed
http://area1.chunktestarea.com/Category/Fashion/abc.aspx ------ OK

The 3rd url is working because *.aspx is mapped to ISAPI on IIS. So, it invokes the Global.asax and the url is matched with the regex!
0
 
LVL 16

Accepted Solution

by:
gnoon earned 500 total points
ID: 20504428
0
 

Author Comment

by:jimbona27
ID: 20504571
ok, i'll see if we can get this checked in iis tomorrow thanks. it works with default.aspx at the end although that cant be assumed it will always be there so i need to check iis to get it sorted. thanks
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20952968
Forced accept.

Computer101
Community Support Moderator
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now