Verify a Password Protected Site with Google (Adsense, Wordpress)

July 22nd, 2008

I previously discussed how to get Google to verify a WordPress blog by making a few modifications to the .htaccess file. I would like to take this a step further and discuss how a password protected site can be verified with Google. Verifying a password protected site became necessary for me when I wanted to add a password protected site to Adsense.

The first thing that needs to be done is to have your server respond with a valid 404 error by adding something like the following to your .htaccess file.

ErrorDocument 404 "This file does not exist."

Now you just need to give Google access to two files with a name similar to google54fbcc37db740a4d.html and noexist_54fbcc37db740a4d.html. You will need to login to Google’s Webmaster Tools Dashboard to see what the google*.html filename is in your case and then replace the “google” part with “noexist_” for the second file. Now that you have the names you will need to modify your password protection code that looks something like the following to exclude the two files from password protection.

AuthGroupFile /dev/null
AuthName "A Blog"
AuthType Basic
AuthUserFile /home/user/domains/domain.com/.htpasswd
require valid-user

To exclude the two files you would add the below code to your .htaccess file.

<FilesMatch "(google54fbcc37db740a4d\.html)|(noexist_54fbcc37db740a4d.html)">
  Allow from all
  Satisfy any
</FilesMatch>

Don’t forget to create the google*.html file on your server and replace the names of the files in the above code with your filenames.

Extra Directions for WordPress

Now the above directions should be enough for most password protected sites but for WordPress that is using permalinks one more thing needs to be done. You need to tell WordPress to not send the request for noexist_*.html to index.php. To do this find the following code in your .htaccess file.

1
2
3
4
5
6
7
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

and add the following RewriteCond after line 5.

RewriteCond %{request_uri} !^/noexist_54fbcc37db740a4d.html$ [nc]

Verify a WordPress Blog with Google

July 21st, 2008

In order to verify your site with google you need to go to Google’s Webmaster Tools Dashboard and add your website and then click verify. After clicking verify it will ask you to put a file on your website with a name similar to google54fbcc37db740a4d.html. In most cases this is all that needs to be done and then google can verify that you are the owner.

If you are using permalinks with WordPress it is not so easy to get Google to verify your site. There are two items that are causing validation to not work with WordPress. First, Google is trying to access more than just google54fbcc37db740a4d.html it is also trying to request a page that does not exist. When I tested it, Google was also asking for noexist_54fbcc37db740a4d.html. You might notice that Google just replaced the word “google” with “noexist_”. Second, WordPress is taking a request for any page (including ones that do not exist) and trying to find an appropriate page and is therefore not returning a 404 error. These two problems cause Google to respond with the following error when you try to validate your site.

We’ve detected that your 404 (file not found) error page returns a status of 200 (Success) in the header.

 The code that causes WordPress to not return a 404 for an invalid page is the following which is in an .htaccess file.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

After creating your file google54fbcc37db740a4d.html and adding it to your server you just have to modify the above .htaccess file to have it not redirect the request for noexist_54fbcc37db740a4d.html to index.php. You also need to specify a specific page or message to display when a 404 error occurs. You can do that by replacing the above code with the following code.

RewriteEngine On
ErrorDocument 404 "This page does not exist"
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{request_uri} !^/noexist_54fbcc37db740a4d.html$ [nc]
RewriteRule . /index.php [L]

Make sure you replace the 54fbcc37db740a4d in the above code with the same characters of the file that google tells you to add to your website.

Password Protect All but One File (htaccess)

July 20th, 2008

Once in a while need to allow someone access to one file but no other files in the same directory. I often solve this problem by moving the one file to a sub directory and then adding the following to an htaccess file in that same sub directory.

allow from all
satisfy any

This normally works well but is not a perfect solution since it is not always appropriate to move the file to a different directory. So, I took the time and figured out how to password protect everything but one file. Below is the basic password protection that I had in the htaccess file that blocked everything in the directory (and sub-directories).

AuthGroupFile /dev/null
AuthName "A Blog"
AuthType Basic
AuthUserFile /path/to/.htpasswd
require valid-user

Now in order to exclude a file I just had to add the following below the above six lines of code.

<Files "page.html">
  Allow from all
  Satisfy any
</Files>

Of course this can be taken one step further if you wanted to exclude multiple files from password protection.

<FilesMatch "(page1\.html)|(page2\.html)">
  Allow from all
  Satisfy any
</FilesMatch>

FilesMatch can take a regular expression so you don’t necessarily have to list out each file. The below code will also accomplish the same thing (as above).

<FilesMatch "page[1-2]\.html">
  Allow from all
  Satisfy any
</FilesMatch>

Count Lines of Code in C#

December 4th, 2007

When I am programming I often wonder how much code I have actually typed. Sometimes when I get in the zone, have a well thought out design and have auto-completion helping, I can generate code pretty quick. Although there are quite a few programs out there that will count the lines of code some are a bit more intelligent than others.

At the most basic level a simple command in DOS can count the lines of code in a file or directory. But, I often want to a little more than just the the number of lines in each file. I want to know things like the lines of code vs empty lines and the number of lines that contain comments. Fortunately, there is a plug-in for Visual Studio that will do just that. Since, I am normally using Visual Studio to write any program in C# this is a very logical place to have this tool.

C# count lines of code

My next mission will be to find a good line counter for Java. Anyone know of any?

What Is the Best Email Client?

December 1st, 2007

Determining the best email client is not a clear cut decision as there are quite a few ways one might manage their email. Although, I do think it is interesting that Thunderbird comes up first when searching Google for the phrase What Is the Best Email Client. I have to admit I agree with Google on this one Thunderbird is really good!

The thing I love most about Thunderbird is it’s IMAP support. I believe IMAP is the best way to read email since the email is kept on the server. This allows the email to be accessed from any computer (home, work, friends house, etc). With that definition it doesn’t sound like it is much different than a web based email client (SquirrelMail, GMail, Hotmail, etc) but it is!

The main feature that makes me believe that Thunderbird is better than a web based email client is the way email accounts are accessed. I can have 10 different email accounts that exist on 10 different servers and only need 1 application (Thunderbird) to access them all. Also, since the application is running on my computer it tends to run a little faster than a web site that will need to reload the page anytime I click something.

GMail does have a good user interface and page load time is pretty quick with their use of ajax. Although, if you are using dial-up Internet you will definitely notice a difference as the first time Google loads it can take quite a while which can be seen in the screenshot below.

GMail slow startup when using dialup

GMail is probably my favorite web based email client but I really like to have more control over my email than what Google gives me. I like to know that if my email starts consuming 100 gigabytes of space that I won’t have a problem. Since I run my own mail server, I like knowing that my email is backed up and where it is backed up. So I don’t have to worry about Google one day saying sorry we lost half of your email. I also like knowing that I have control over my email and Google can’t one day decide to hold my email for ransom (not that I really believe Google would).

Google has recently started offering IMAP support (where they were only offering pop). I have been waiting for that feature for a few years and am very glad that that started supporting it. Now I can access my GMail account in Thunderbird too!

So, what about other email clients like Outlook, Outlook Express, Windows Mail. Although, I do like a lot of the features that Outlook has like managing appointments and contacts. It’s support for IMAP is pretty bad. Since IMAP is what I like using there really is no good competition.

Actually, I can think of one other email client that I have considered moving to in the past due to its speed and quick access keys. That client is Pine developed by the University of Washington. It is a text only email reading client and it supports IMAP. I love how fast I can read my email’s in Pine and I also love that I don’t have to wait for large HTML email’s to load or worry about some hidden link (phishing) or virus that could be hidden in the HTML. Unfortunately, I also like reading some email’s that come in an HTML only format. So, that one reason alone is enough for me to not use it.

That said, I would love to hear what email client you are using and why you have chosen to use it?

Extra Update Center Module – Netbeans 6 beta 2

October 25th, 2007

In the nightly build of netbeans there is a module that can be installed called “Extra Update Center Module”. This can be made available in Netbeans 6 beta 2 by going to Tools–>Plugins–>Settings and clicking the “add” button. Then use the following url:

http://deadlock.netbeans.org/hudson/job/javadoc-nbms/lastSuccessfulBuild/artifact/nbbuild/nbms/updates.xml.gz

Click “ok” and you should now have access to the “Extra Update Center Module”. Of course the plugins that are provided by this module could be unstable. That is why it is only included with the nightly builds.

Extra Update Center Module

touch with DOS commands

March 20th, 2007

I often use Cygwin to get access to a few linux style commands that I need. The command touch is one of those commands. In case your not familiar with it it will create/update the creation and modification times of a file to the current time. If the file specified does not exist it will also create an empty file.

The problem I recently encountered though was I needed the touch functionality but on a computer that did not have Cygwin installed. Of course I could have just put together a quick little C# or Java program to do the trick but I still would have to install the app onto this other computer.

The solution to this delimia was to use the following dos command which will create an empty file:
copy /y nul file.txt

Count number of lines in a file using DOS

February 25th, 2007

Every once in a while I get on a computer and I need to count the number of lines in a file. My first instinct is to open my text editor (editplus) and hit ctrl+end to get to the bottom of the document. Then I can view the status bar which will tell me the line number. This works fine when I am on my computer but not when I am on another computer that does not have editplus installed.

My next option might be to open this file in notepad and do the exact same thing. This will work fine if the file is not to large. The problem is I often deal with very large files. I need a quicker way to produce the same results.

This is where DOS comes into play. I can use the following command and let DOS quickly tell me the number of lines in the file.

findstr /R /N “^” file.txt

This command will output every line with a line number in front of it but will still take a long time given a very large file. The solution to this is to take this command one step further.

findstr /R /N “^” file.txt | find /C “:”

Now the output will only be the number of lines that are contained in the file.Again, this command could be taken a step further to tell you how many lines are in the file that contain a certain string.

findstr /R /N “^.*certainString.*$” file.txt | find /c “:”

I’m sure there are many other great uses for find and findstr. If you have found one please post comment.

Regular Expression Search and Replace via Command Line (Editplus)

December 14th, 2006

Editplus is definitely my favorite text editor but it does not entirely support Regular Expression. Below is an example of when Editplus has failed me:

Editplus-regex

So, I used the regular expression .*<table.*\n.*<tr>\n.*<td>. This unexpectedly returned everything from the beginning of the file down to the <td>. Basically it seems to have a little bit of trouble with multi-line regular expressions.

My solution to this problem was to develop a little regular expression command line program and add it as an Editplus user tool. An added benefit to creating this application is I can now do Regular Expression search and replace via the command line. At the time of creating this application I did not know about Powershell as that would have provided the same functionality. But this little application works so I will stick with it for now.

So, how does the search and replace program work? If you type SearchReplace.exe at the command line you will get the following:

Usage: searchReplace.exe “regular expression” “replacement string” file.txt

Pretty simple ehh?  Note: if you happen to have double quotes in your regular expression they will need to be escaped.

Now to set this up with Editplus just go to Tool —> Preferences —> User Tools and put in the following values:

Command: SearchReplace.exe
Argument: “$(Prompt)” “$(Prompt)” $(FileName)
Initial: $(FileDir)
Check: Run as text filter

When this user tool is run the first pop up box will be where you put the regular expression and the second will be the replacement string.

At this point I have to give a little warning that I have not thoroughly tested this program and I am not liable for any data loss caused by this program. If you happen to encounter any problems let me know. 

Determine File Encoding

December 14th, 2006

I ran into a problem where I wasn’t sure if I could trust the encoding that my text editor was displaying. I also wanted to check the encoding on about 10,000 files and determine if it was not a specific encoding. So, I created a little program to tell me the encoding of a file. This in combination with powershell allowed me to loop recursively through all the files in a directory and create a list of the files that were not the correct encoding.

A Good portion of the code that I used for this application was available on the another site that describes how to determine the Encoding of a Unicode file.

The application can be downloaded below:

DOWNLOAD: Determine File Encoding