Jump to content

  • Log in with Facebook Log in with Twitter Log In with Google      Sign In   
  • Create Account

Subscribe to HRA Now!

 



Are you a Google Analytics enthusiast?

Share and download Custom Google Analytics Reports, dashboards and advanced segments--for FREE! 

 



 

 www.CustomReportSharing.com 

From the folks who brought you High Rankings!



Photo
- - - - -

Another Redirection Issue


  • Please log in to reply
42 replies to this topic

#31 kblm

kblm

    HR 2

  • Members
  • PipPip
  • 13 posts

Posted 11 April 2007 - 04:21 PM

QUOTE(chrishirst @ Apr 11 2007, 03:40 PM) View Post
unbalanced paranthesis is the problem biggrin.gif

there is an extra ")" that snuck in there


Thanks Chris....I can't believe I didn't catch that. I looked at that code and a couldn't find a problem to save my life! Isn't it always the little things....unmatched brackets, or missing quotes.

Anyway....thanks for the code. Works great!

-Kevin

#32 anirudh

anirudh

    HR 1

  • Members
  • Pip
  • 1 posts

Posted 15 April 2007 - 04:01 PM

A very basic solution. It is tedious but thought could be useful for people who cannot work on custom 404s or cannot have one.

putting a 303 for eg.

/oldfile.htm -> /newdir/newfile.asp

delete /oldfile.htm and create a directory named /oldfile.htm/ and create a new file inside this new directory /oldfile.htm/default.asp

add this code in default.asp
CODE
<%@ Language=VBScript %>
<%
' Permanent redirection
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", "/newdir/newfile.asp"
Response.End
%>


voila. a very easy and simple solution.

#33 Tiggerito

Tiggerito

    HR 2

  • Members
  • PipPip
  • 49 posts
  • Location:Adelaide, South Australia

Posted 16 April 2007 - 04:13 AM

QUOTE(chrishirst @ Mar 7 2007, 09:36 AM) View Post
Ok retested the code because this shouldn't happen because no array match should drop through to the ... else thus displaying the error page and return the 404 response to the User Agent.

It transpires in VbScript that when you run an instr() comparison against a empty variable it returns a value of 1 rather than 0 which would be expected. So the script returns bFound as True even though there is no match.


so Redirect XP (Ver 3.21) is below
CODE
<%
dim i, newURI, req, pageArray(1,50)

pageArray(0,0) = "/dir/pagename.htm"
pageArray(1,0) = "/dir/pagename.asp"

bFound = false

for i = 0 to ubound(pageArray,2)
if pagearray(0,i) <> "" then          
     if instr(request.servervariables("QUERY_STRING"), pageArray(0,i)) <> 0 then
         req = replace(request.servervariables("QUERY_STRING"),"404;","")
           newURI = replace(req,pageArray(0,i), pageArray(1,i))
            bFound = true
      end if
end if
if bFound then exit for

next

if bFound then
' uncomment the next line to keep the original URI but show the new content
'server.transfer("/newdir/" & pageArray(1,i))
'server.transfer(pageArray(1,i))
' uncomment the next lines to redirect to the new pagename
    response.status = "301 Moved Permanently"
    response.addheader "Location", newURI
    response.end()
else
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Not Found</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
This is the 404 page
<body>
</body>
</html>

<%
    response.status = "404 Not Found"
    response.end
end if
%>
Test to destruction to see if I have still missed something.

To answer the point about having :80 in the URL
Technically it would make no difference to accessing the site , however it would be a different URL to search engines so would create the same issues as having default.asp in the URLs or the www vs non-www issues.


As a heads up, I don't think you can include query strings when using server.transfer

I found this made it a non option in my case (I was trying to create friendly urls).

#34 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 6,938 posts
  • Location:Blackpool UK

Posted 16 April 2007 - 05:03 AM

A server.transfer operation does just that,

transfers everything to the referenced page

All the request collections are available. Even variables that are set in the preceeding page are transfered with their values intact.

Probably what leads you to that conclusion, is that the querystring from the 404 handler is not transfered in the URI, BUT it is there and available from the request.servervariables collection.

www.blooberi.com (adult products) runs on my custom 404 handler using server.transfer for ALL of the products.

10 script pages and a lot of includes run the whole site.


#35 Tiggerito

Tiggerito

    HR 2

  • Members
  • PipPip
  • 49 posts
  • Location:Adelaide, South Australia

Posted 16 April 2007 - 07:16 PM

QUOTE(chrishirst @ Apr 16 2007, 07:33 PM) View Post
A server.transfer operation does just that,

transfers everything to the referenced page

All the request collections are available. Even variables that are set in the preceeding page are transfered with their values intact.

Probably what leads you to that conclusion, is that the querystring from the 404 handler is not transfered in the URI, BUT it is there and available from the request.servervariables collection.

www.blooberi.com (adult products) runs on my custom 404 handler using server.transfer for ALL of the products.

10 script pages and a lot of includes run the whole site.


Reference: Server.Transfer Method @ http://msdn2.microso...y/ms525800.aspx

Check the remarks section:

"If the path you specify in the input parameter is for an .asp file in another application, the .asp file executes as if it were in the application that contains the Server.Transfer command. In other words, all variables and objects that have been given application scope either by other .asp files in the application or by the application's Global.asa file are available to the called .asp file. However, the path parameter must not contain a query string, or ASP returns an error."

This might imply only asp files have this limitation, I'm not sure.

#36 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 6,938 posts
  • Location:Blackpool UK

Posted 17 April 2007 - 01:35 AM

QUOTE
This might imply only asp files have this limitation, I'm not sure.
Nope, it's quite correct
Try using server.transfer("/path/filename.ext?param=value") and ASP will throw an error.

However. I don't recall saying that the querystring was passed as a querystring in the address.

QUOTE
the querystring from the 404 handler is not transfered in the URI, BUT it is there and available from the request.servervariables collection


AND as you can see here;
CODE
     if instr([b]request.servervariables("QUERY_STRING")[/b], pageArray(0,i)) <> 0 then
         req = replace([b]request.servervariables("QUERY_STRING")[/b],"404;","")




#37 Tiggerito

Tiggerito

    HR 2

  • Members
  • PipPip
  • 49 posts
  • Location:Adelaide, South Australia

Posted 17 April 2007 - 01:51 AM

QUOTE(chrishirst @ Apr 17 2007, 04:05 PM) View Post
Nope, it's quite correct
Try using server.transfer("/path/filename.ext?param=value") and ASP will throw an error.

However. I don't recall saying that the querystring was passed as a querystring in the address.



AND as you can see here;
CODE
     if instr([b]request.servervariables("QUERY_STRING")[/b], pageArray(0,i)) <> 0 then
          req = replace([b]request.servervariables("QUERY_STRING")[/b],"404;","")


So how would I use server.tranfer to do the following url redirect:

From: page/friendly-name.htm
To: file-generator.asp?page=friendly-name

As page=friendly-name is not part of the original uri (i.e. in the server variable) , I can't see how the receiving file can get the information it needs.

#38 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 6,938 posts
  • Location:Blackpool UK

Posted 17 April 2007 - 04:16 AM

aha, the penny drops.

not so much as how to transfer it but how to parse the information out of it once it is there.

OK

This has to be done in the receiving script (or an include script) and obviously will depend on exactly how you have created the "friendly" URI in the first place.

for mine, the page address is built from the database as

/[category]/[productgroup]/en/[productid]/ the "en" here is just a place holder.

Obviously the productname can be left off to get to the group, and the productgroup can be left off to get to the category.

there is also an addition for when larger product pictures have been uploaded (the script checks for them first and displays the link if they exist) of
/[category]/[productgroup]/extrapic/[productid]/

The parsing of the passed querystring from the server variables is done by a split (on "/") into an array so it contains all the elements from the URL (the "http://" is left out with a mid() string slice)

arrayname(lbound(arrayname)) will always have the hostname

and depending on how many array elements there are I can get each of the relevant parts.
if arrayname(ubound(arrayname)-1) is "en" it's a product page, if it's "extrapic" then it's the larger pic page to show. So some variables are set and do a server.transfer to the relevant page or include the page content script. It's then a very simple matter of building a SQL query to pull the relevant information

with only 1 or 2 array elements it has to be a product category or a product group.

If after all that, the item is not found it will deliver a 404 response to the UA.


For your example;

in filegenerator.asp

CODE
qString = request.servervariables("query_string")

VFolders = split(mid(qString,instr(qString,"://")+3),"/") ' VFolders is now a array of the "virtual folder names" from the 404 querystring


and you would pass VFolders(ubound(VFolders)) on to the script that pulled the appropriate page.

so wherever you used request.querystring("page") replace it with VFolders(ubound(VFolders))


Obviously there is more code for error checking and validating the decoded parameters etc. Even a rewritten URL could be used for a SQL injection attack if a "cracker" worked out what any of the "folder" names actually meant.



#39 geekspeek228

geekspeek228

    HR 1

  • Members
  • Pip
  • 1 posts
  • Location:Biloxi, MS

Posted 24 May 2007 - 12:42 AM

chrishirst-

I have been reading through this thread and find it all to be exactly what I may need for my site. The code you've provided should do nicely.

One question and sorry if I missed this while reading ... does the redirect code go inside the custom 404 page that I currently have on my site? Or is the code supposed to go in another page? My 404 page is currently in the root of my web so, I assume that it stays there and the array points to the .aspx pages that I want .htm pages to redirect to.

Oh, can my 404 page remain as .htm? Or will I need to have my IIS manager point to a new .aspx 404 page?

Thanks for taking the time to explain.

Will

#40 Tiggerito

Tiggerito

    HR 2

  • Members
  • PipPip
  • 49 posts
  • Location:Adelaide, South Australia

Posted 24 May 2007 - 01:23 AM

QUOTE(chrishirst @ Apr 17 2007, 06:46 PM) View Post
For your example;

in filegenerator.asp

CODE
qString = request.servervariables("query_string")
  
   VFolders = split(mid(qString,instr(qString,"://")+3),"/") ' VFolders is now a array of the "virtual folder names" from the 404 querystring


and you would pass VFolders(ubound(VFolders)) on to the script that pulled the appropriate page.

so wherever you used request.querystring("page") replace it with VFolders(ubound(VFolders))


Obviously there is more code for error checking and validating the decoded parameters etc. Even a rewritten URL could be used for a SQL injection attack if a "cracker" worked out what any of the "folder" names actually meant.


I see.

That would work for my example. I would probably have to implement some GetVariable() function in the receiving page that could handle all the different ways data can be passed (server variable, querystring or form variable). I already do the other two ways so my pages work with GET and POST transparently.

It does mean I would not be able to do customisable regex based re-mapping of urls within the 404 as the only information that can be passed to the receiver is the original url.

Maybe the way would be to have the 404 page decide the correct receiving page and have the receiving page refine the mapping further using regex on the server variable to determine any variable values.

#41 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 6,938 posts
  • Location:Blackpool UK

Posted 24 May 2007 - 07:38 PM

QUOTE
One question and sorry if I missed this while reading ... does the redirect code go inside the custom 404 page that I currently have on my site? Or is the code supposed to go in another page? My 404 page is currently in the root of my web so, I assume that it stays there and the array points to the .aspx pages that I want .htm pages to redirect to.

Oh, can my 404 page remain as .htm? Or will I need to have my IIS manager point to a new .aspx 404 page?

Either in the 404 page directly or as an include.

the 404 has to have an extension that is parsed for asp code and the error page type set to URL. So you will need your application mappings have .htm to be passed to the asp interpreter.


#42 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 6,938 posts
  • Location:Blackpool UK

Posted 24 May 2007 - 07:49 PM

QUOTE(tiger)
Maybe the way would be to have the 404 page decide the correct receiving page and have the receiving page refine the mapping further using regex on the server variable to determine any variable values.


That's the way I handle it

the variables are DIMmed and initialised in the 404 script page and passed over to the recieving script.

The one thing to avoid is passing required values in sessions or cookies, otherwise you will find the scripts fail for crawlers etc.





#43 chrishirst

chrishirst

    A not so moderate moderator.

  • Moderator
  • 6,938 posts
  • Location:Blackpool UK

Posted 28 May 2013 - 10:13 AM

I never did get around to the .NET version (yet), but I have recently done a PHP version for someone.

 

The brief was to redirect a hundred plus URLs with no pattern that could be used to identify the new destination URL, so ather than have a longish list of RedirectMatch lines in .htaccess that have to be loaded, read and tested on EVERY request, we went down the script route. With this you can simply use any part of the requested URI that is unique and after the TLD.

The value of the associative array, which becomes the destination URL can be relative or absolute so can be used as a "jump" script should you so wish.

<?php
$URIs = array(
    "128_163" => "/blankets/", "128_164" => "/mobiles/"
);

$request = $_SERVER['REQUEST_URI'];

foreach ($URIs as $req => $dest ) {
    $goto = '';
    if (inStr($req, $request)) {
    $goto = $dest;
        header("HTTP/1.1 301 Moved Permanently");
        header("Location:".$goto);
    break;
    }
}
if ($goto  == '') {
    header("HTTP/1.1 404 Not Found");
}


function inStr ($find, $inWhat) {
  $findLen = strlen($find);
  $i = 0;
  for($i=0; $i < strlen($inWhat); $i++)
  {
    if(substr($inWhat, $i, $findLen) == $find)
    {
      return TRUE;
    }
  }
  return FALSE;
}  
?>

 

 

And you get a handy PHP inStr()  function thrown in to the bargain.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

SPAM FREE FORUM!
 
If you are just registering to spam,
don't bother. You will be wasting your
time as your spam will never see the
light of day!