.htaccess – Force www redirect

I recently found that having domain.com and http://www.domain.com on your website would affect SEO (Search Engine Optimization). It’s recommended to use either. So I chose the latter. This can be easily done by having a .htaccess rule on your root directory.

# Force www redirect #
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301,NC]

301 is the HTTP status code for a Permanent Redirect which doesn’t affect the SEO ratings.

(98)Address already in use: make_sock: could not bind to address [::]:80

I tried to reproduce how the system got in this situation and wasn’t able to. I did find the system in the same state as you reported it, but I couldn’t figure out how it got like that.

Basically when you ran “apachectl start”, Apache was already running. When Apache is being started (ie: apachectl start), it first checks to see if it is already running by checking or a PID file (Process ID file). If it sees a file, it will report this:

staging-aops:/var/run# apachectl start
httpd (pid 23390) already running

In this case, Apache *WAS* running, but there was NO PID file to be found, so Apache assumed it was OK to start. So it began to start but noticed that it couldn’t bind to port 80 because that port was already in-use by another process (which was Apache), and that’s the error message you received:

(98)Address already in use: make_sock: could not bind to address [::]:80

So what I had to do was manually kill Apache (given there wasn’t a PID file so it didn’t think it was even running), then I started Apache, noticed it created a PID file, then I stopped Apache (and noticed the PID file was deleted, as it was supposed to be). I then started Apache again and it created the PID file just fine and started.

I was not able to reproduce the problem where Apache was running yet the PID file didn’t exist. I am not sure how this is happening. The PID file is located at: /var/run/apache2.pid. Did someone delete the PID file in an effort to fix a problem? Sometimes that is required. If Apache were to be killed or somehow die and the PID file didn’t get cleaned up (deleted), then the PID file will have to be manually deleted before Apache can be started. But in this case it was the reverse – Apache WAS running w/no PID file.

To fix this in the future, you can:

1)   ps -ef | grep apache2 | grep root | grep -v grep    (which should output something like this):

         root     23741     1  0 13:54 ?        00:00:00 /usr/sbin/apache2 -k start

NOTE: The reason I grepped for “root” was because Apache uses the ROOT user for the initial parent process. This gives Apache the authority to do its job, running as user ROOT, and each subsequent Apache process that is spawned (child process) is now running as the “www-data” user. In order to kill all Apache processes in one step, you want to kill the parent (which is the only one running as user “root”). Killing that PID should also kill all child processes

The number to the RIGHT of “root” (23741, in this example), is the PID#. You can then do:

2)  kill -9 23741  (PID will vary each and every time you perform these steps, so do not try and reuse the 23741 number but the actual number from step# 1
3)  rm /var/run/apache2.pid      (Make sure NO PID file exists - it shouldn't, but just in case)
4)  apachectl start   

If you have this issue again, going through those 4 steps should surely fix the problem.

Apache is running now and there is a PID file now – so all should be good. You can “apachectl stop”, “apachectl start” or “apachectl restart” and all are working now.

Terminate local bottle server

I had my local bottle server running and my Terminal shut down. After reboot, I wasn’t able to start the server again because it was in use. I had to kill the open port connection. My bottle was running on 8081 so I searched for protocols using that port:

netstat -nap | grep 8081
tcp   0    0*      LISTEN      6491/python

Next I killed the PID by executing:

kill -9 6491

Force https on apache

Force https on apache or redirect http traffic to https site. Here’s a simple configuration that can be done within the .htaccess file on the site directory (do this in the site’s root directory if https is to be enforced for the entire site).

<IfModule mod_rewrite.c>
  RewriteEngine on
  Redirect permanent / https://%{HTTP_HOST}%/
  RewriteRule %{HTTPS} !=on
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Alternatively, you can modify the site configuration under /etc/apache2/sites-available/non-https-site

Add php code to .html file types

I wanted to add some php code to an existing static html page. But I noticed that the PHP content didn’t render on the browser in the first place. It would only work if I renamed the file extension from .html to .PHP.

In order to have the php content work in an HTML file extension, the following piece of configuration is required within the .htaccess file on your server. This can be created on the site’s root directory and the properties are inherited by all the sub-directories.

AddType application/x-httpd-php .html

Configure bottle framework for Apache

Follow these steps to port over your local bottle framework onto an apache server that would run on http 80

1. Install the bottle framework on the server

2. Install mod_wsgi on the server

sudo apt-get install libapache2-mod-wsgi

This will install a .so module in Apache’s module directory:


It will also automatically configure Apache to load the mod_wsgi module upon restart. You can confirm the presence of the module in Apache’s available modules directory… as well as Apache’s enabled modules directory:



More details about installation and configuration on http://webpy.org/cookbook/mod_wsgi-apache-ubuntu

3. Under the apache configuration – add a new file under sites-enabled and call this restapi. The contents of the restapi file would be like this:

<VirtualHost *:80>
        ServerName      server.domain.com
        ServerAlias     www.your-alias.domain.com
        ServerAlias     www.your-alias.domain.net
        ServerAlias     www.your-alias.domain.pvt

    DocumentRoot    /var/www

        WSGIScriptAlias /               /var/www/restapi/restapi.wsgi
        WSGIDaemonProcess       restapi    threads=25
        WSGIScriptReloading     Off

    <Directory /var/www>
        Order allow,deny
        Allow from all

    <Directory /var/www/restapi>
        WSGIProcessGroup restapi
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all


4. Navigate to the restapi directory and create a file called restapi.wsgi. The contents of the restapi.wsgi should be something like this:

import sys, os, bottle

sys.path = ['/var/www/restapi/'] + sys.path

# Change working directory so relative paths (and template lookup) work again

import apiserver         # This loads the REST framework that you have implemented as apiserver.py (The file that handles get/post requests)

# ... build or import your bottle application here ...
# Do NOT use bottle.run() with mod_wsgi
application = bottle.default_app()