[Apache] Discard QUERY_STRING

Apache_mod_rewrite is a lot of fun but sometime it syntax is very “twisted”. For example let say you want to make a conditional rewrite on the url, query string included. You will probably write something like this:

RewriteCond %{HTTP_HOST} ^www.foobar.com$
RewriteCond %{QUERY_STRING}  ^queryType=3&item=services&itemId=47$
RewriteRule ^/initial/cms/php/workflow.php?$ http://www.foobar.com/Home/Our-services/Business [NC,R=301,L]

Now problem right ? Yep except as the destination url doesn’t have it own query string mod_rewrite will keep the original query string ! Damned. To prevent this, write instead this:

RewriteCond %{HTTP_HOST} ^www.foobar.com$
RewriteCond %{QUERY_STRING}  ^queryType=3&item=services&itemId=47$
RewriteRule ^/initial/cms/php/workflow.php?$ http://www.foobar.com/Home/Our-services/Business? [NC,R=301,L]

or append the QSD flag.

Stat command examples

stat displays the detailed status of file using inode data. Theses info include the date the file was last modified (like ls), the time the file was last changed and the time that file was last accessed:

# stat /var/lib/dpkg/lock 
  File: `/var/lib/dpkg/lock'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d      Inode: 57030       Links: 1
Access: (0640/-rw-r-----)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2011-08-13 12:17:00.000000000 +0200
Modify: 2012-03-08 16:03:20.000000000 +0100
Change: 2012-03-08 16:03:20.000000000 +0100

stat can also be use to get information on a partition, using the -f option:

# stat -f /dev/sda1 
  File: "/dev/sda1"
    ID: 0        Namelen: 255     Type: tmpfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 2560       Free: 2514       Available: 2514
Inodes: Total: 6181154    Free: 6180411

And don’t forget the most useful option for scripting --format:

# stat --format "%A" /var/log/syslog
-rw-r-----

Create a SSH tunnel at boot time

To mount persistent SSH tunnel, nothing is better than autossh. But there is a small inconvenient, it doesn’t really like being started in a runlevel (go figure why). The solution is to use the /etc/rc.local file for this task:

autossh -i /foobar/.ssh/id_dsa -L 83:192.168.3.32:80 root@moya.farscape.net -oBatchMode=yes -N

Here we make a persistent tunnel with a forwarding of local machine port 83 to the moya.farscape.net machine port 80, using the user foobar key to initialize the connection. autossh will monitor the ssh tunnel and restart it as needed.