Category Archives: Knowledge Base

.htaccess: redirect URLs containing php queries

I recently had to solve the problem of redirecting URLs with php queries, i.e. http://www.phillab.net/?page=programme&subpage=Passdraw → http://www.hasper.info/passdraw/ by using the .htaccess file.

Since most of the URLs did not have such a 1:1 translation like the example above, I did not work with wildcards – but this is also possible! I had the following constraints

  1. The php queries have to be removed from the redirected URL (this is not the standard!)
    RewriteEngine On
    RewriteCond %{QUERY_STRING} subpage=passdraw
    RewriteRule (.*) http://www.hasper.info/passdraw/? [R=301,L] #The question mark in the end does the trick!
    
  2. The php query check should be case-insensitive
    RewriteEngine On
    RewriteCond %{QUERY_STRING} subpage=passdraw [nocase]
    RewriteRule (.*) http://www.hasper.info/passdraw/? [R=301,L]
    
  3. Some queries have to point to the same new URL
    RewriteEngine On
    RewriteCond %{QUERY_STRING} subpage=passdraw [nocase,or]
    RewriteCond %{QUERY_STRING} subpage=bestAppEver [nocase]
    RewriteRule (.*) http://www.hasper.info/passdraw/? [R=301,L]
    
  4. Some queries contain special characters like dots or plus signs.
    RewriteEngine On
    RewriteCond %{QUERY_STRING} subpage=Ghostball\x2B3[nocase] #A plus sign has to be replaced by \x2B
    RewriteRule (.*) http://www.hasper.info/ghostball-3/? [R=301]
    RewriteCond %{QUERY_STRING} subpage=N\.T\. [nocase] #A dot has to be escaped by a slash
    RewriteRule (.*) http://www.hasper.info/n-t-a-commercial/? [R=301,L] #The L marks this as last rule
    

How to install and run an APK on Google Glass

Since Google Glass is currently only available for “Explorers”, the usability is quite limited and the device requires some hacking. Since the Glass-specific OS is updated regularly this will change soon but up until then, the following trick will come in handy:

Install and run an APK on Google Glass:

Well, currently the standard Glass launcher is quite limited and only allows for starting some pre-installed applications. You could either install a custom launcher like Launchy or do it without any modifications at all: In a nutshell, we will install the APK, find out the name of the app’s launching activity and then launch it. So no modifications and no prior knowledge of the application’s code is needed.

  1. Connect the Google Glass to your computer (you should have the Android SDK installed) and enable the debug mode on your device.
  2. Open a terminal and install the APK via adb install <apk path>.
  3. Find the android tool aapt (located in <sdk>\build-tools\<version>).
  4. Retrieve the package name and the activity to launch: aapt dump badging <apk path> | grep launchable-activity.
  5. Now you have all necessary information to launch the activity whenever you want: adb shell am start -a android.intent.action.MAIN -n <package name>/<activity name>.
  6. As an example with the password app Passdraw (currently not ported to Glass 🙂 ): adb shell am start -a android.intent.action.MAIN -n com.passdraw.free/com.passdraw.free.activities.LandingActivity

What I learned during the redesign

I have decided to update my website and turned it into a blog. All “portfolio” content is now presented as articles and is alternating with some tricks and tips mostly related to IT. This post is both the announcement of the new design and a collection of snippets I found while customizing WordPress and the theme.

  1. The sticky header is achieved by setting
    .imageBadge {
    	background-attachment:fixed;
    	min-height: 100px;
    }
    .textBadge {
    	position:fixed;
    }
    .mainContent {
    	z-index: 2;
    	position: relative;
    	background-color: #FFF;
    }
    
  2. The parallax effect for the header is done like this:
    /**
     * Creates a parallax effect for the header when scrolling
     */
    (
    function () {
    	var a = document.getElementById("masthead"),
    		e = $(window);
    	e.unbind("scroll").scroll(function () {
    		a.style.backgroundPosition =  "center "+ -(e.scrollTop()/ 9) + "px";
    	});
    }
    )();
    
  3. The sticky footer or “the curtain reveal effect” is done with
    .footerContainer {
      height:50px;
    }
    .footer {
      position:fixed;
      bottom:0px;
      padding: 10px 0px;
      z-index:0;
      width:100%;
    }
    

    A minimal demonstrator for points one to three can be found here: http://jsfiddle.net/PQS2C/3/

  4. Since there are so many syntax highlighters out there: I prefer the SyntaxHighlighter for WordPress. It does not screw up the indents, it has line numbering and visitors can easily copy the code.
  5. Protecting your WordPress login page (or other sensitive documents) by using .htaccess is a good idea because it reduces the load when your site is attacked and you have an additional protection against security vulnerabilities. But if you want a specific IP address to be excluded from the password query so you don’t have to enter the password every time, add the following to your .htaccess in the WordPress root folder:
    <Files wp-login.php>
      AuthUserFile <path to your .htpasswd>
      AuthName "Admin console"
      AuthType Basic
      require valid-user
      Order allow,deny
      allow from 12.34.567.89
      satisfy any
    </Files>
    # Guard some other sensitive files
    <FilesMatch "(\.htaccess|\.htpasswd|wp-config\.php|readme\.html)">
      order deny,allow
      deny from all
    </FilesMatch>
    

How to connect Google Glass to Windows

I recently got my hands on a Google Glass, the Android-based head-mounted display developed by Google. While connecting to it and installing apps works like a charm on my Linux system, it was quite a hassle to do the same with Windows.

I found a quite nice tutorial which I had to adapt to Windows 8: In a nutshell, we have to convince the Google usb driver that it fits to the Glass device and due to the editing we have to convince Windows 8, that it is okay for the driver’s signature to mismatch. Please proceed at your own responsibility.

  1. Connect the Google Glass to your PC and watch how the driver installation fails. If it does work: congratulations, you are done!
  2. Note the VID and the PID of your connected Glass. You can find them via Device Manager → Portable Devices → Glass 1 → Properties → Details → Hardware Ids.
  3. Open <sdk>\extras\google\usb_driver\android_winusb.inf
  4. Add the following lines using the VID and PID from step 2 to sections [Google.NTx86] and [Google.NTamd64]:
    ;Google Glass
    %SingleAdbInterface% = USB_Install, USB\VID_0000&PID_0000&REV_0216
    %CompositeAdbInterface% = USB_Install, USB\VID_0000&PID_0000&MI_01
    
    %SingleAdbInterface% = USB_Install, USB\VID_0000&PID_0000&REV_0216
    %CompositeAdbInterface% = USB_Install, USB\VID_0000&PID_0000&MI_01
    
  5. Go to the device manager and update the drivers.
  6. If you are not running Windows 8, you are done. If you are, the following error will occur: “The hash for the file is not present in the specified catalog file. The file is likely corrupt or the victim of tampering”. This is because we have altered the .INF-file and now the signature does not match anymore.
  7. Go back to the file android_winusb.inf and search for the lines
    CatalogFile.NTx86   = androidwinusb86.cat
    CatalogFile.NTamd64 = androidwinusba64.cat
    

    and comment them out:

    ;CatalogFile.NTx86   = androidwinusb86.cat
    ;CatalogFile.NTamd64 = androidwinusba64.cat
    
  8. Now, you will get a different error: “The third-party INF does not contain digital signature information”. Well, this security check is great but since we know what we are doing … : Do an “Advanced Startup” (just press the windows key and type it in, then go to Troubleshoot → Advanced options → Start up settings → Restart.
  9. Disable driver signature enforcement in the boot menu.
  10. Update your drivers again in the device manager and this time skip the driver signature enforcement.
  11. Google Glass should now be recognized correctly. Restart your computer if you want to re-enable the driver signature enforcement.

Force Facebook to show a specific image when linking to your site

With the time shift due to the end of daylight savings coming up soon I faced the same question I face everytime: Will I have more time in the morning or less? Since I’m definitely not the only one always struggling with this I came up with a silly website: kannicheinestundelängerschlafen.de which answers the question “Can I sleep one hour longer?” with a simple “Yes” or “No”.

When someone would post this link on Facebook I wanted to make sure that a meaningful image was assigned to it. But the website does not even contain an image, so what to do?

Just use the Open Graph API and tell Facebook, which image to use – and while we are at it also manipulate the title and description. Add the following lines to the head node in your HTML document:

<head>
	<meta property="og:description" content='This is the description' />
	<meta property="og:title" content='And this is the title' />
	<meta property="og:image" content='http://example.com/image.jpg' />
</head>

P.S.: As I noticed, Facebook still struggles with German umlauts in domain names. Sometimes it would show the url with umlauts and sometimes without. Even explicitly stating the information like

<meta property="og:url" content='http://www.kannicheinestundelängerschlafen.de' />

could not resolve this. I hope this will be fixed soon.

GRUB: Reboot to a specific OS

I have installed Linux parallel to Windows since I still need Windows for image and video editing. Because Linux is my main system, I’ve set the timeout of GRUB to one second so I don’t waste a lot of time but can still change the OS to boot if I am quick enough. But when I reboot from Linux to switch to Windows, I always miss this one second :-). So instead I run the following script, placed under /usr/local/bin:

#!/bin/bash
grub-reboot 2 #The entry's number to start
reboot

To make this work, you have to set the default entry to “saved”:

GRUB 1: In /boot/grub/menu.lst , set default to saved.

GRUB 2: In /etc/default/grub , set GRUB_DEFAULT=save