Developer forum

Forum » Development » 301 redirects

Reply
I have made a notification subscriber module which is supposed to handle 301 redirects.
But it appears that when a page doesnt exist, my subscriber isnt invoked.
Any idea what causes this and how to handle it?

Sincerely Sune Fengel

Replies

 
Nicolai Høeg Pedersen
Reply
If a page does not exist, a pageview object is not made and it cannot raise any notifications that comes from within the pageview object...

What notification are you using?

And what do you wan't to accomplish - then I might find another solution.

Be aware, that Dynamicweb with 7.0 changed all 302 redirects from pageview object to be 301.
 
Reply

I have a massive loop that should redirect a non existing url to a new specific url (vi moved the site from a php solution to a DW solution).

something like: 

if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(
                    "/version3/destinationer/dansk-vestindien/dansk-vestindien-fakta.php"))
{
 HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", "http://www.stjernegaard-rejser.dk/Dansk-Vestindien/Infomation-om-landet/Fakta-og-klima.aspx");
}

I use the Dynamicweb.Notifications.Standard.Page.Loaded
 
Nicolai Høeg Pedersen
Reply
Try using Notifications.Standard.Application.BeginRequest

It might not work, but alternatively you should add you piece of code to Application_OnPreRequestHandlerExecute in Global.asax
 
Reply
ok I tried using Notifications.Standard.Application.BeginRequest. Unfortunately didn't work though. 
How do I add my code to the global Asax file?
I can see that it inherits from Dynamicweb.Admin.Global. 
Do I just write my snippet in the .asax file itself?
 
Nicolai Høeg Pedersen
Reply
Yes - just write it inside the global.asax.cs/vb file in that handler. Just remember to keep the line that calls Dynamicweb version of the same handler...
 
Reply
I dont understand. 
How do I compile my own global.asax.cs file into the project?

 
Nicolai Høeg Pedersen
Reply
If you have a custom project, you would have your own Global.asax in the root of that project...

I've just mailed you a global.asax and global.asax.cs you can compile into your project.
 
Reply
Thanks for the code.
I didnt have a custom project, just a lot of modules.
But I created a CustomModules.dll and changed the application tag to 
<%@ Application Codebehind="Global.asax.cs" Inherits="CustomModules.Global" %>.

I managed to write out a testline when I go to the root of the site.

But when I enter a wrong url like: /version3/destinationer/kenya_med_undersider/kenya-hoteller.php it doesnt reach the Application_OnPreRequestHandlerExecute event.

So the global ajax thing is working, but alas I'm back to square 1 :(.

Any other suggestions?

- Sune
 
Nicolai Høeg Pedersen
Reply
I take it that you have this problem in your local installation? Not tested on the live site?

requests for .php files are not routed by default into the .NET isapi - Dynamicweb handles this by using its own 404 handler (/Admin/Public/404.aspx) which is setup in IIS manager.

How to set that up can be found in installation guide - also sending you a screen dump of the setup of the live site.

You should be able to catch this 404 from global.asax...
 
Reply
I only tested it on the live site I have no up-to-date local installation.
 
Nicolai Høeg Pedersen
Reply
OK - just looking into a solution - will be back, as that cool muscle guy once said...
 
Nicolai Høeg Pedersen
Reply

Try this bit of code - and work from there...


Sub Application_OnPreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
  If HttpContext.Current.Request.Url.ToString().ToLower().Contains(".php") Then
   Dim url As String = HttpContext.Current.Request.Url.ToString().ToLower()
   'For test:
   'HttpContext.Current.Response.Write(url)
   'HttpContext.Current.Response.End()
   'Entering http://basicmodules.local.dynamicweb.dk/DoesNotExist.php in the browser
   'Give me something like this:
   'http://basicmodules.local.dynamicweb.dk/admin/public/404.aspx?404;http://basicmodules.local.dynamicweb.dk:80/doesnotexist.php

   'To handle:
   If url.Contains("doesnotexist.php") Then
    HttpContext.Current.Response.ClearHeaders()
    HttpContext.Current.Response.Status = "301 Moved Permanently"
    HttpContext.Current.Response.StatusCode = 301
    HttpContext.Current.Response.StatusDescription = "Moved Permanently"
    HttpContext.Current.Response.AddHeader("Location", "DoesExist.aspx")
    HttpContext.Current.Response.AddHeader("X-CUSTOM-MSG", "From my own 404 handler bit") 'This header can be seen with Fiddler tool - for debugging
   End If
  End If
  GlobalAsax.Application_OnPreRequestHandlerExecute(sender, e)
 End Sub

 
Nicolai Høeg Pedersen
Reply
By the way - Dynamicweb have a feature for this.

You can use the direct paths module from modules tab (Dynamicweb.NET) or management center (Dynamicweb 7).

If you do not want to type that in by hand - fill the table containing the information. Think it is pretty self explaining.

UrlPathID [Integer] UrlPathPath [String] UrlPathRedirect [String] UrlPathStatus [Integer] UrlPathCreated [Date] UrlPathUpdated [Date] UrlPathActive [Boolean]
1 welcome.php welcome.aspx 301 2/21/2008 10:36:16 AM 2/21/2008 10:36:16 AM True
2 en Default.aspx?ID=58 301 4/4/2008 11:14:06 AM 4/4/2008 11:15:30 AM True

What this does is redirecting "UrlPathPath" to "UrlPathRedirect" using "UrlPathStatus" as status code. UrlPathStatus can be 301, 302 or 200.

You can easily add records like this:

Dim up As New Dynamicweb.UrlPath.Path
   up.UrlPathPath = "someoldlink.php"
   up.UrlPathRedirect = "NewLocation.aspx"
   up.UrlPathStatus = 301
   up.Save()

 
Reply
That could have been the solution if we had been aware of this feature from the beginning. But it concerns a couple of hundred urls, so I myself wouldnt want to be the one who inserts them :).

Regarding your previous post I did the following test based on the code you sent me:

public void Application_OnPreRequestHandlerExecute(object sender, EventArgs e)
        {
            if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(".php"))
            {
                string url = HttpContext.Current.Request.Url.ToString().ToLower();            
              
                if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("version3/hovedmenu/rejsemaal.php"))
                {
                    Base.w("!Exist");
                }
            }
            GlobalAsax.Application_OnPreRequestHandlerExecute(sender, e);
        }
But still it doesnt appear to raise this event.
Ill just give it a test with the direct urlpath and check with my boss if it is something we should continue with instead.

- Sune
 
Nicolai Høeg Pedersen
Reply
Base.w("") it not enough...

The 404 handler has already taken over, and will display something - depending on how you set up 404 in Control Panel.

To see it work you would have to end the request:
Base.we("Found and stopping request")
 
Reply
OK
I tested and it worked so it looks like were gonna go with the Direct paths module on this one.

thanks for your help anyway :).

- Sune

 

You must be logged in to post in the forum