Wednesday, March 4, 2009

Problems with 404 error, Query string, ASP and international (Hebrew) characters

EDIT: Fixed a typo in the code where I had "QUERY_STRING" instead of "HTTP_URL"

Today I had to move a website for a client. The website is very old, and is written in ASP, and was hosted on a shared host.

The former developer wanted to have SEO friendly URLs but didn't have a URL Rewrite module available.

What he did was making a custom 404 error page, that would redirect the SEO URL and take care of the error if the URL shouldn't be redirected. As part of the script, he took the QUERY_STRING and processed it like so:

Urlstring = Request.ServerVariables("QUERY_STRING")

Response.Write UrlString
Response.Write URLDecode( UrlString )

Long story short, when moved, this code started to result in a very weird behavior. The QUERY string of international URL's turned to "???" question marks. After checking the settings for IIS, and making sure all the correct code pages are set, I found out that the behavior is changing depending if the text was before or after the '?' in the URL.

For instance, the following URL:

If you replaced 'something.asp' with international characters, they would turn to question marks, but if you replaced 'something=something' with international characters they would display properly.

After some trial and error the solution was to use "HTTP_URL" instead of "QUERY_STRING" and strip out a part of it, like so:

Urlstring = Request.ServerVariables("HTTP_URL")

Set RegularExpressionObject = New RegExp
With RegularExpressionObject
.Pattern = "\/err404\.asp\?"
.IgnoreCase = True
.Global = True
End With
UrlString = RegularExpressionObject.Replace(UrlString, "")
Set RegularExpressionObject = nothing

Response.Write UrlString
Response.Write URLDecode( UrlString )

Of course, this is just a quick and dirty RegExp example, and I wouldn't advise using RegExp because they're expensive on memory and resources, but I think you're good to go from here.

Any other ideas? Feel welcome to ask questions or comment!

Friday, February 20, 2009

What content would you like to see next?

Hey guys. I was just wondering - what content would you like to see posted next? Is there any particular subject of interest or question on anyone's mind?

Feel free to answer in the comments...

Tuesday, February 17, 2009

Don't wanna be Obama's Elf

How to update a single field in CakePHP

Here's a quick line of code to demonstrate how to update a single field in CakePHP.

Suppose we will be updating a field named 'confirmed' in a model called 'User' with the id of 5. We will then have an array with just that value, like this:

$data = array(
'User' => array(
'id' => 5,
'confirmed' => 1

We should then pass it on to a save function:

$this->User->save( $data, false, array('confirmed') );

You can RTFM about the save() function here.

Wednesday, February 11, 2009

How to undelete text files on linux

Today was a hard day. Someone accidentally deleted a bunch of files with code containing the last two days' work.

Fortunately, all the files were successfully recovered. I'll share the way that worked here with you.

First of all, the moment you lose your text data, make sure that if you can, you unmount the drive the files were on, so you won't overwrite the lost data.

Then you will have to run a utility called strings. Basically what it does is extracting all string data from it's input, and outputs it to STDOUT or whatever output you specify.

We will tell it to read our partition on the hard drive the lost files were. (Learn more about Linux partitions) and output it to a file on another partition.

So the command should look something like this:
strings /dev/sda1 > /some/path/for/output.txt

Note that '/dev/sda1' should be replaced by your partition path, and you should provide a path to where you want the output file to be. (NOTE: AVOID WRITING IT TO THE SAME PARTITION AT ANY COST. THE RESULTING FILE WILL BE HUGE, MORE THAN 10GB, AND YOU DON'T WANT TO OVERWRITE YOUR PRECIOUS DATA!).

After the process finishes (takes quite a while), you should have a HUGE text file that contains ALL of the text from your hard drive, deleted or not.

Now, basically all you need to do is to grep and locate the text you lost inside the whole mass.

Hope it helps you! It sure helped me!

Tuesday, February 10, 2009

Linux - working with filenames and directories starting with a - (dash)

I use SVN daily on my Linux development server, but today I ran into a silly problem that took me some figuring out.

If you try to work with filenames that start with - (dash) such as "-filename.ext", you will get errors such as:
svn: invalid option character: i
Type 'svn help' for usage.

After some looking around, the solution for it was, instead of:
svn rm -filename.ext

To pass '--' as the first argument:
svn rm -- -filename.ext

Hope it helps anyone out there.