RewriteRule and Trailing Slashes

Just a quick post today, i needed to redirect a URL with and without a trailing slash to the same destination, but didn’t want to create two separate rules, so I did some searching and found that is is very simple.

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^video(/)?$ http://video.somedomain.com [R=301,L]

The (/)? is the key to matching a url with or without a trailing slash.

mailto: Email Link Obfuscation (Hiding)

After searching for a good email link obfuscation script that allows the address to be seen as normal text i came upon a webpage that had a good solution, so i did a little reverse engineering and created this PHP script to output the same code, enjoy!

<?php
/**
 * @author Josh Hartman
 * @copyright 2010
 */
 
function strToHex($string, $prepend=null)
{
    $hex='';
    for ($i=0; $i < strlen($string); $i++)
    {
        $hex .= $prepend.dechex(ord($string[$i]));
    }
    return $hex;
}
 
function createRandomPassword() {
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
    while ($i <= 7) {
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}
 
function mailto_link($email){
    if(preg_match('/^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\\.)+[A-Z]{2,4}$/i',$email)){
        $var1 = createRandomPassword();
        $var2 = createRandomPassword();
        $email = explode('@',$email);
        $user = strToHex($email[0],'%');
        $domain_array = array_reverse(explode('.',$email[1]));
        $domain = "'";
        foreach ($domain_array as $k=>$v){
            $domain_array[$k] = strToHex($v,'%');
        }
        $domain = "'".implode("','",$domain_array);
        $domain .= "'";
        $output = $var1."=['".$user."',[".$domain."].reverse().join('.')].join('@');".$var2."=unescape(".$var1.");document.write(".$var2.".link('mai'+'lto:'+".$var1."));";
        $output = "<script type=\"text/javascript\">eval(unescape(\"".strToHex($output,'%')."\"));</script>";
        echo $output;
    }else{
        return false; //email address is not valid
    }
}
 
mailto_link('username@domain.com'); //use this function wherever you want an email link
?>

Simple Standalone PHP Web Counter

In my search for a simple PHP web counter it was difficult to find one that wasn’t somehow connected to an external host and didn’t write IP addresses to a file and then use resources to search the file later.  With all the NAT firewalls out there counting visitors based on IP address just doesn’t seem like a modern thing to do. I took a script by Eric Sizemore and modified it to create my own version.  What it does is add to the visitor count (stored in a txt file on the server) if they don’t have a cookie showing that they have visited before, then i set that cookie to expire after 60 days, after that if the visitor comes again they will be counted again.  It’s not the most accurate, but gives a good idea of how many people are visiting and i wanted to reduce server file operations.

The Code

Download the Code (ZIP)

Installation & Usage

  1. Unzip the code into your website’s root folder
  2. Change permissions on the counter log txt file so that it is writable (chmod 666).
  3. Add the following PHP code at the top of each root page you would like to display the counter on:
    <?php include('counter/counter.php'); ?>
  4. Add the following PHP code where you want to have the counter display:
    <?php show_counter(); ?>
  5. Test the page you’ve included counter.php on and have show_counter() displayed.

Installing Metadot on Ubuntu Server 9.04

Installing Ubuntu 9.04 on Virtual PC

Note: If not installing to Virtual PC, ignore any steps that have to do with GRUB, menu.lst and modifying the boot command line.

1. Boot to installation CD

2. Choose a language (English)

3. Press F6 and then ESC

4. Add “vga=791 noreplace-paravirt” before “quiet” in the boot command line and press ENTER

5. Choose a language (English)

6. Choose a country (United States)

7. Detect keyboard layout (No)

8. Origin of keyboard (USA)

9. Keyboard layout (USA)

10. Choose hostname (metadot6)

11. Choose time zone (Central)

12. Choose partition method (Guided – use entire disk and setup LVM)

13. Choose hard drive to partition (SCSI1 (0,0,0) – (sda))

14. Write the changes to disk and setup LVM (Yes)

15. Choose amount of volume group to use for guided partitioning (max)

16. Write the changes to disk (Yes)

17. Enter Full Name for new account

18. Enter Username for account

19. Enter Password for account

20. Encrypt your home directory (No)

21. Choose proxy (leave blank)

22. Choose upgrade management (Install security updates automatically)

23. Choose software to install (LAMP, OpenSSH, Samba)

24. Enter MYSQL root password

25. Installation is complete, continue to reboot automatically. IMPORTANT: Read next three steps before continuing

26. During boot, when GRUB menu shows, press ESC, E, and E again to edit the boot line

27. Add “vga=791 noreplace-paravirt” before “quiet” in the boot command line

28. Press ENTER and B to boot

29. Login with your admin account

30. Run: sudo nano /boot/grub/menu.lst

31. Find normal kernel boot command line in bottom part of file and add “vga=791 noreplace-paravirt” before “quiet”

32. Ctrl+O, ENTER and Ctrl+X to save and exit

33. Run: sudo reboot

 

Configure a Static IPv4 Network Address

1. Login with your admin account

2. Run: sudo nano /etc/network/interfaces

3. Remove the “iface eth0 inet dhcp” configuration line under the “# The primary network interface” section

4. Replace with the following:

iface eth0 inet static
address 192.168.1.10
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1

5. Ctrl+O, ENTER and Ctrl+X to save and exit

6. Run: sudo /etc/init.d/networking restart

7. Test pinging 192.168.1.10 from another PC

 

Install Latest Ubuntu Updates

1. Login with your admin account

2. Run: sudo apt-get update

3. Run: sudo apt-get upgrade

4. Run: sudo apt-get dist-upgrade

5. What to do with menu.lst (keep the local version)

6. Run: sudo reboot

 

Configure MySQL Server

1. Login with your admin account

2. Run: sudo nano /etc/mysql/my.cnf

3. Comment out the lines “skip-external-locking” and ”bind-address = 127.0.0.1″

4. Ctrl+O and Ctrl+X to save and exit

5. Run: mysql -uroot -p mysql

6. Enter MySQL password

7. Run the following SQL commands:

update user set Host='%' where Host='127.0.0.1' and User='root';
FLUSH PRIVILEGES;
quit

8. Run: sudo /etc/init.d/mysql restart

9. Test connecting with HeidiSQL

 

Install Metadot6

1. Connect to the server with an SSH client and then login with your admin account

2. Run: sudo apt-get clean

3. Run: sudo apt-get install libapache2-mod-speedycgi speedy-cgi-perl libwww-perl libdbi-perl libapache2-mod-perl2 libapache-dbi-perl libxml-rss-perl libnet-ldap-perl liblog-agent-perl libmail-pop3client-perl libio-stringy-perl libmailtools-perl libmime-tools-perl libunicode-string-perl libspreadsheet-writeexcel-perl libdate-calc-perl libappconfig-perl libimage-size-perl libtemplate-perl liblingua-en-nameparse-perl libnumber-format-perl libxml-simple-perl libtext-csv-xs-perl libarchive-zip-perl libdate-manip-perl libxml-dumper-perl libhtml-tree-perl libhtml-format-perl

4. Run: sudo apache2ctl restart

5. Run: sudo -i

6. Run: useradd -m metadot

7. Run: passwd metadot

8. Enter password (metadot)

9. Run: chsh -s /bin/false metadot

10. Run: cd /home/metadot

11. Run: wget http://download.metadot.com/v6/Metadot6.4.5.4.tar.gz

12. Run: tar -zxvf Metadot6.4.5.4.tar.gz

13. Run: chown -R www-data.www-data metadot

14. Run: find . -type d -exec chmod 755 {} \;

15. Run: find . -type f -exec chmod 644 {} \;

16. Run: chmod -R o+w /home/metadot/metadot/sitedata

17. Run: chmod -R o+w /home/metadot/metadot/html

18. Run: chmod 640 /home/metadot/metadot/etc/metadot.conf

19. Run: exit

20. Run: sudo mysql -uroot -p mysql

21. Enter MySQL password

22. Run the following SQL commands:

INSERT INTO 'user' VALUES ('localhost','metadot',password('metadot'),'Y','Y','Y','Y','Y','Y','N','N','N','N','N','Y','Y','Y','N','N','N','N','N','N','N','N','N','N','N','N','N','N','','','','','0','0','0','0');
FLUSH PRIVILEGES;
CREATE DATABASE 'metadotdb';
use metadotdb;
SOURCE /home/metadot/metadot/default_data/sampledata.sql;
quit

23. Run: sudo /etc/init.d/mysql restart

24. Run: sudo nano /home/metadot/metadot/etc/metadot.conf

25. Change “pass=changeme” to “pass=metadot”

26. CTRL+O, ENTER and CTRL+X to save and exit

27. Run: sudo nano /etc/apache2/sites-available/metadot

28. Use the following configuration:

<VirtualHost *:80>
 
	ServerAdmin webmaster@localhost
 
	DocumentRoot /home/metadot/metadot/html
 
	<Directory /home/metadot/metadot/html>
 
		DirectoryIndex index.html index.php
 
		Options Indexes FollowSymLinks MultiViews
 
		AllowOverride None
 
		Order allow,deny
 
		allow from all
 
	</Directory>
 
	ErrorLog /var/log/apache2/error.log
 
	# Possible values include: debug, info, notice, warn, error, crit,
 
	# alert, emerg.
 
	LogLevel warn
 
	CustomLog /var/log/apache2/access.log combined
 
	Alias /images/ /home/metadot/metadot/html/images/
 
	Alias /js/ /home/metadot/metadot/html/js/
 
	Alias /metadot/ /home/metadot/metadot/metadot/
 
	Alias /public/ /home/metadot/metadot/sitedata/public/
 
	Alias /skins/ /home/metadot/metadot/sitedata/skins/
 
	# mod_perl2 goodness
 
	PerlOptions +Parent
 
	PerlOptions -ParseHeaders
 
	PerlSwitches -Mlib=/home/metadot/metadot/metadot
 
	<Perl>
 
		use Cwd;
 
		chdir "/home/metadot/metadot/metadot/";
 
		1;
 
	</Perl>
 
	<Location /metadot>
 
		DirectoryIndex index.pl
 
		SetHandler speedycgi-script
 
		PerlHandler ModPerl::Registry
 
		Options +ExecCGI
 
	</Location>
 
</VirtualHost>

29. Run: sudo a2dissite default

30. Run: sudo a2ensite metadot

31. Run: sudo apache2ctl restart

32. Test going to http://192.168.1.10 with a browser on another PC

 

Additional Resources

Download metadot user and admin guide (PDF)
Skin cookbook (DOC)