Apache: Convert URL parameter strings to directories for search engine friendly URLS

Imagine you have a URL like http://www.whatever.com/?language=en which looks pretty ugly. For Search Engine Optimization it would be nice to have a more search engine friendly URL like http://www.whatever.com/en.

Apache offers an easy way to hide the parameter string by the second URL format. However, internally the page still is treated like the first version of the URL but users and search engines will not notice the old URL format.

To implement the example from above you simply have to add the following lines to your httpd.conf .

In this example the directory en is forwarded internally to page.php?language=en. If the user enters http://www.whatever.com/en he will see the content of http://www.whatever.com/page.php?language=en without noticing the parameter string.


<Directory "/documentroot/en">

  Options Indexes FollowSymLinks

  AllowOverride All

   Order allow,deny
   Allow from all

  Options +FollowSymlinks
  RewriteEngine on

  RewriteCond %{REQUEST_URI} =/en/

  RewriteRule   ^/? page.php\?language\=en [PT]

</Directory>

In the line /documentroot/en you define the directory for which you want to apply the hack. It has to be the internal location at your server.

The line “RewriteCond %{REQUEST_URI} =/en/” tells Apache to apply the hack only if http://www.whatever.com/en is requested. Other URLs in the directory like http://www.whatever.com/en/anypage.html will not be touched.

Advertisements

How to use Apache as a proxy server

The following configuration allows you to make Apache forward the request for your domain to a second domain at port 8081. If the user enters “www.domain.com” in his browser he well see the content provided by the server listening at http://www.otherdomain.com:8081/. 


<VirtualHost *:80>
    ServerName www.testdomain.com
    ServerAlias testdomain.com *.testdomain.com
    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
 
    ProxyPass / http://www.testdomain.com:8081/
    ProxyPassReverse / http://www.testdomain.com:8081/
    <Location />
        Order allow,deny
        Allow from all
    </Location>
</VirtualHost>


Wt C++ Web Toolkit: How to send parameters to a slot using the boost::bind routine

Since I did not find any documentation for this in the web: If you are using Wt C++ Web toolkit and ever wondered, how to send parameters to a SLOT() there is a simple solution using the boost::bind routine.

Imagine you have an button, and an event that requires one parameter then you can setup the whole scenario like this:

void MyClass::test(std::string parameter)
{
  cout << parameter << endl;
}

WPushButton *button = new WPushButton("Test", this);

button->clicked().connect(boost::bind(&MyClass::test, this, "Hello World!"));

The step which is often forgotten is to put the instance of the class right after the function pointer. (In this case “this”)

Thats it!

Execute Script on Connection established using Network Manager Dispatcher

If you want a script to be executed each time a Network connection is established there is a easy way to do so with Linux distributions which use the Network Manager (like Fedora, Suse, Ubuntu etc.). You simply have to copy the script to the directory /etc/NetworkManager/dispatcher.d and change the permissions to 755. (It is crucial to use 755, otherwise the script will be ignored.

chmod 755 /etc/NetworkManager/dispatcher.d/10-my-scirpt

The scipts in this directory are executed by the order of their name eg. 10-my-script will be executed before 20-my-script.

Setup Webserver Monitoring with Munin

The tool Munin offers an easy way to monitor a webserver. It comes along with a lot of features like monitoring disk and memory usage, the CPU, the fork rate of new processes and much more. The monitored data is accessible by the web browser. The setup of Munin takes only a few minutes.

First you have to install Munin. In Fedora Linux this can be done via YUM:

yum install munin munin-node

Once installed you have to edit the configuration file /etc/munin/munin.conf (The default file contains a lot of comments). For a basic setup you can add the following lines:


dbdir /var/lib/munin
htmldir /var/www/html/munin
logdir /var/log/munin
rundir  /var/run/munin


[www.example.com]
    address 127.0.0.1
    use_node_name yes

In this case “/var/www/html/” has to be the HTML directory of you webserver. What is missing now is to create the HTML directory for Munin and set the correct permissions:

mkdir -p /var/www/html/munin
chown munin:munin /var/www/html/munin

Restart Munin

/etc/init.d/munin-node restart

The monitoring is running now. The results can be accessed by the webbrowser:

http://www.djvu-pdf.com/munin/

By default there is a .htaccess password protection for this page activated. You can either remove the file /var/www/html/munin/.htaccess or create a username password combination like follows:

You can create the required password file (usually /etc/munin/munin-htpasswd, it is quoted in the .htaccess file) like follows

htpasswd -c /etc/munin/munin-htpasswd Admin

Where Admin is the username. You will be asked for the password in a prompt.

Munin produces intuitive graphs of the monitored resources like the following one:

Clone Hard Disk with Command Line Tools

If you have two hard disks in you machine and you want to be one and exact copy of the other one you can use the tool “dd” to do so.

Lets say you want /dev/sdb to be a clone of /dev/sda, then you can simply use the command:

dd if=/dev/sda of=/dev/sdb

Make sure, that /dev/sdb has at least the same size as /dev/sda!

How long does dd take?

Cloning a hard disk with dd can take several hours.

Assuming a average speed of 8 MB/s copying a disk with 120 MB would take about four hours.

Send Server Ip Address by Email

This post describes to configure a web server in a way that it sents its IP address by email in a defined interval of time. This can be useful for example if your server has a dynamic IP address that changes with the time. In this example the tool Postfix is used to sent the emails instead of sendmail since it works similar and is way easier to configure than sendmail.

Preparations:

1. Install Postfix

In a first step it is necessary to setup and install postfix. You can download the source code here but most recent linux distributions include the package already. In Fedora the setup can simply be done by:

yum install postfix

2. Configure Postfix

You need an SMTP server in order to sent the emails. If you have an email account with SMTP Server you can add the following lines to the postfix configuration file /etc/postfix/main.cf :

relayhost = smtp.provider.com:25
 
smtpd_sasl_auth_enable = yes
smtpd_sasl_path = smtpd
smtp_sasl_password_maps = hash:/etc/postfix/passwd
smtp_sasl_type = cyrus
smtp_sasl_auth_enable = yes

Now the password file /etc/postfix/passwd has to be created. The password file has to contain the line

smtp.provider.com:25 username:password

To convert the password file in a so called postmap look-up table do the following:

postmap hash:/etc/postfix/passwd

If

postmap -q smtp.provider.com:25 /etc/postfix/passwd

gives you the correct reply everything is working fine.

3. Test Postfix
To test this configuration, postfix has to be started

postfix start

Postfix now provides a sendmail command you can use like follows to test your setup:

Create a simple text file test.mail:

From: you@provider.com
message
(compulsory blank line at the end)

and execute the command

sendmail -f you@provider.com -s recipient@provider.com < test.mail

The mail should have been sent now. If something is not working errors can be found in the following file:
/var/log/maillog

If you find something like

warning: SASL authentication failure: No worthy mechs found

status=deferred (SASL authentication failed; cannot au
thenticate to server smtp.provider.com: no mechanism available)

you probably have to add the following line to your /etc/postfix/main.cf:

smtp_sasl_security_options = noanonymous

Howto sent the IP by email:

To sent the IP address of you server by emil you simply you have execute the following simple script send_ip.sh by a cron job.

#!/bin/bash
# send_ip.sh

wget http://checkip.dyndns.com/ -O ip.txt
sendmail -f you@provider.com -s recipient@provider.com < ip.txt
rm -f ip.txt

http://checkip.dyndns.com/ can be replaced by any web page that is displaying your IP.

The only thing that is missing now is to setup a cron job that executes the script in a defined interval of time, lets they every six hours.

As root do:

crontab -e 

and add the following line:

0 */6 * * * source /home/user/send_ip.sh ;

Now start the crond and thats it!

/sbin/service crond start
chkconfig crond on

Attention!
You have to make sure, that the postfix mailing system is always running. In case to ensure that postfix sendmail command is not interferring with the “normal” sendmail command stop it.

service sendmail stop
postfix start

Multiple Domains on one IP Address using Apache Virtual Server

Using Apache Virtual Servers it is easy to make multiple domains point at the same IP address displaying different content. You just have to add the following lines to the file /etc/httpd/conf/httpd.conf and restart apache:

NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.first-domain.com
ServerAlias first-domain.com *.first-domain.com
DocumentRoot /var/www/html
</VirtualHost>

<VirtualHost *:80>
ServerName www.second-domain.com
DocumentRoot /var/www/samples
</VirtualHost>


In this setup http://www.first-domain.com is displaying the content of /var/www/html while http://www.second-domain.com is displaying the content of /var/www/samples.

SSH Tunnel as Socks Proxy Server for the Web Browser

If you have SSH access to a remote host and you want to use it as proxy server for your web browser you can do so easily by opening up a SSH tunnel:

ssh -D 9999 username@host.com -N

If you now set your socks proxy in your web browser to localhost, port 9999 your traffice is redirect via host.com. In firefox you can do so in Preferences->Network->Settings

PHP Multi File Uploader with Progress Bar using APC

Important Update: If you are using PHP versions greater 5.3 you should use our new multi-file uploader script which does not depend on the APC cache. You find it here:

https://mydailyhacks.wordpress.com/2014/11/05/php-multifile-uploader-for-php-5-4-5-5/

For older PHP versions just continue reading.

This little PHP Script allows you to upload multiple files at a time to a webserver. It is pretty easy to install and can easily be adapted and extended to your specific needs. The Ajax progress bar the script brings is based on jQuery. For a proper operation of the script the Alternative PHP Cache (APC) has to be activated.

Where to get the script?

How to install APC on a linux server?

In Fedora Linux you need the following packages to install APC using the pecl command afterwards.


yum install pcre-devel  php-pear php-devel httpd-devel

pecl install apc

The file /etc/php.ini should contain the following lines:


extension=apc.so
apc.enabled = 1
apc.max_file_size = 2000M
apc.rfc1867 = 1

An introduction how to install it from the source code you find here.
http://www.electrictoolbox.com/install-apc-php-linux/

If you are not sure if APC is installed at your server contact the administrator.