Categories
.htaccess Tutorials & Tips Apache Web Server Tutorials & Tips Content / SEO Tips & Tutorials Web Applications & Online Software

<IfModule></IfModule> in .htaccess

I was debugging about some mod_rewrite errors caused by the .htaccess file on one of my sites and couldn’t solve the problem myself so I opted for a help thread on the SitePoint.com forum. Turned out it’s not the problem of my mod_rewrite rules in .htaccess but some incorrect file permissions that were causing the trouble.

Dklynn was very nice to help me in this regard and offered a gold advice that I thought I should share with you.

You Should NOT Use <IfModule></IfModule>. Why?

He pointed out that I should get rid of the <IfModule> conditional from the .htaccess file. It’s useless and a complete waste of server resources by all means. No excuses.

Considering the fact that all HTTP requests (including those to trivial assets such as .css, .js, .gif, .jpg, etc., thus each web page download would trigger about 10 or even more HTTP requests) to the Apache web server are handled by the rules in your .htaccess file, I think I should get rid of all such <IfModule> conditionals from all .htaccess files on all my sites, or the server would be doing useless check of “If Module xxx Is Enabled” every time a HTTP request is received…..

With a site receiving 1000 page visits per day, your server would have to do 10,000 such useless checks at the cost server resources and performance.

Many famous 3rd party scripts such as WordPress has <IfModule></IfModule> in the .htaccess file by default. This is to prevent potential errors should the module was not installed on the client’s production site / server. You should try and remove it. If the mod_rewrite and SEO friendly URLs are working, you can remove it without any problems.

This is pure gold advice offered by Dklynn. Check out his site about Apache mod_rewrite.

Categories
Apache Web Server Tutorials & Tips PHP Tips & Tutorials SQL / MySQL Tips and Tutorials

How to bring down / optimize memory usage in your unmanaged Linux VPS box and avoid OOM (Out Of Memory) errors?

The other day I was very upset about some extraordinary down times of my unmanaged VPS box at Linode. As it’s unmanaged, support staff at Linode are not responsible for the failures. I contacted them and they told me it’s OOM (Out Of Memory), pointing me to the right documentation to figure out how to get the problem sorted out myself. After a few tweaks and observations for a week, so it seems that I have successfully optimized my VPS server to take on more traffic with less resources such as RAM.

The problem almost always lies in where the user is free to feed stuff to your website or program. Sometimes Convert Hub spikes in memory usage and forces my box to use swap that relies on disk I/O to work. This happens when someone uploads an ultra large picture to be processed or converted. While I may restrict the size of the picture that is allowed to be uploaded, I may also do the following settings to optimize the entire LAMP environment so the other websites enjoy it as well.

Apache 2 Low-Memory Optimization

Use this command to identify the MPM you are using:

apache2 -V | grep 'MPM' # for Debian-based systems
httpd -V | grep 'MPM' # for Fedora/CentOS systems

Find and change these settings in your Apache 2 configuration file (usually found at /etc/apache2/apache2.conf):

StartServers 1
MinSpareServers 3
MaxSpareServers 6
ServerLimit 24
MaxClients 24
MaxRequestsPerChild 3000

Switch to Lighttpd or Litespeed if possible.

MySQL Low-Memory Optimization

Same as above, find and change these settings of the MySQL configuration file (may be at /etc/mysql/my.cnf) accordingly:

key_buffer = 16K
max_allowed_packet = 1M
thread_stack = 64K
table_cache = 4
sort_buffer = 64K
net_buffer_length = 2K

PHP Low-Memory Optimization

Find your PHP configuration file (php.ini) and modify the PHP script memory limit to 32M or less (default is 128M):

memory_limit = 32M
Categories
Apache Web Server Tutorials & Tips LAMP Developer Books

Best Books of Apache Web Server to Learn Apache and Use It

As the most popular web server for hosting websites, Apache is second to none. It thrives for a reason, that is, it’s versatile and adapts to all situations and answer to even the slightest demands of a website. Below are some books from Amazon about administering Apache that may give you a new perspective and upper hand in handling the web server.

Web Developers / Designers’ Books:

  1. Best HTML Books
  2. Best CSS Books
  3. Best JavaScript Books
  4. Best PHP Books
  5. Best MySQL Books
  6. Best Linux Books
  7. Best Apache Books (mod_rewrite Books)
  8. Best Web Hosting Books

Apache Cookbook: Solutions and Examples for Apache Administrators

Apache Cookbook Solutions and Examples for Apache Administrators

Apache 2 Pocket Reference: For Apache Programmers & Administrators (Pocket Reference (O’Reilly))

Apache 2 Pocket Reference For Apache Programmers & Administrators (Pocket Reference (O'Reilly))

Apache Security

Apache Security

Apache Administrator’s Handbook (Developer’s Library)

Apache Administrator's Handbook (Developer's Library)

Linux Apache Web Server Administration, Second Edition (Craig Hunt Linux Library)

Linux Apache Web Server Administration, Second Edition (Craig Hunt Linux Library)

Categories
Apache Web Server Tutorials & Tips HTTP Tips & Tutorials PHP Tips & Tutorials

Apache, PHP: Get Client Browser HTTP Request Headers Information

Every HTTP requests made by any client web browsers to an Apache should conform to the HTTP specification and provide certain set of headers information for the server to parse and understand. Useful headers information that can be retrieved in PHP by function apache_request_headers() includes:

  1. User-Agent: Operating System, browser and its version number, …
  2. Accept-Language: Requesting client language
  3. Accept-Charset: Character set of the client
  4. …

To get an array of the above client request headers information, just use apache_request_headers() function:

$headers = apache_request_headers();

And you’ll get:

Array
(
    [Host] => localhost
    [User-Agent] => Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 GTB5
    [Accept] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    [Accept-Language] => en-us,en;q=0.5
    [Accept-Encoding] => gzip,deflate
    [Accept-Charset] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
    [Keep-Alive] => 300
    [Connection] => keep-alive
    [Cache-Control] => max-age=0
)

Similarly, you can use apache_response_headers() to get the HTTP response headers information sent from your server to the client.

Categories
Apache Web Server Tutorials & Tips PHP Tips & Tutorials

Apache, PHP: Function to Get and Return PHP Version Number and Apache Version

To get the PHP version as well as the Apache version of the current host build, you will need the PHP function apache_get_version():

$ver = apache_get_version();

Sample output:

Apache/2.2.6 (Win32) PHP/5.2.5

Which returns a string containing the Apache version number (2.2.6) as well as that of PHP (5.2.5).

You can also get the PHP version by:

echo phpversion();
Categories
Apache Web Server Tutorials & Tips PHP Tips & Tutorials

Apache, PHP: Get Apache Enabled Modules in PHP Dynamically and Detect if a Apache Module is Installed

Sometimes you will need to detect if a certain Apache module is installed dynamically from PHP so as to determine for proper actions to take. For example, if you write inherent functionalities for optional SEO friendly URLs, you will want to know if the client host has the famous Apache module mod_rewrite installed and enabled, or you know it’ll fail.

To get a list (more precisely, an array) of enabled modules in the current Apache installation, run in PHP:

$amods = apache_get_modules();

Let’s display the list:

print_r($amods);
// output
Array
(
    [0] => core
    [1] => mod_win32
    [2] => mpm_winnt
    [3] => http_core
    [4] => mod_so
    [5] => mod_actions
    [6] => mod_alias
    [7] => mod_asis
    [8] => mod_auth_basic
    [9] => mod_authn_default
    [10] => mod_authn_file
    [11] => mod_authz_default
    [12] => mod_authz_groupfile
    [13] => mod_authz_host
    [14] => mod_authz_user
    [15] => mod_autoindex
    [16] => mod_cern_meta
    [17] => mod_cgi
    [18] => mod_dir
    [19] => mod_env
    [20] => mod_imagemap
    [21] => mod_include
    [22] => mod_isapi
    [23] => mod_log_config
    [24] => mod_mime
    [25] => mod_negotiation
    [26] => mod_rewrite
    [27] => mod_setenvif
    [28] => mod_userdir
    [29] => mod_php5
)

You can then use in_array() function to check if a certain Apache module is installed and enabled.

Categories
.htaccess Tutorials & Tips Apache Web Server Tutorials & Tips

Set Expiration or Expiring Time by mod_expires.c on Apache via .htaccess to Reduce Web Page Loading Time

When your site’s ready in design and majority of common media resources won’t change for quite some time, say, half a year, for example, the images and flashes. It makes sense to set the expiration of them much longer than the default to prevent the client browsers downloading them every time a visitor drops by, thus conveniently decreasing the page loading time and increasing user experience.

So how do we do that? On Apache web server, examine, customize and put the following section of .htaccess directives in the document root of your site:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 3 days"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType application/x-javascript "access plus 1 week"
</IfModule>

In case you don’t know what a .htaccess file is, just create a text file named .htaccess (yes, just a dot with the elongated extension), put the above code in, save it and upload the file to the root directory of your domain.

Of course, that said, you will need mod_expires module explicitly enabled as it’s not a core feature but an extension.

Categories
Apache Web Server Tutorials & Tips Business and Marketing

Log Walking – Walk through your website logs and be in the shoe of a visitor

Well, just came across a rather insightful post by ronburk at Webmaster World, which by the way, is probably the most resourceful place for any veteran webmaster.

Consumer behavioral analysis is one of the most important part of any marketing campaigns in any industry. The better you get to know your customers, the better you know how to satisfy them and reach for their wallets. It’s the same with website marketing, whether you are selling your own products or you are advertising for others.

Step into the shoes of others

Stepping into the shoe of others is an essential business skill – a decent skill for a mutually satisfactory social life too.

  1. You step into the shoe of a customer to really know what they need and what they want while they dont’t know it.
  2. You step into the shoe of a business partner on certain terms and see how you can come to a win-win solution, not just beneficial to either one of you.
  3. You step into the shoe of your employee and make them truly love what they do instead of being a tyrant.
Log walking

Log walking, by ronburk, is essentially the process of analyzing server logs for visitor behaviors and applying the found patterns to your business benefits. To decide on what you are going to do such as changing website layout, writing an article, supplying a specific product or service or targeting a certain angle of prospects, you need to first check and think thoroughly of your business environment (market niche, competition, consumer base, advertising fierceness) ,second measure between current situation and your desired objective and third take actions to ax out the changes relentlessly.

Change is inevitably the only eternal thing in the universe, what worked a year ago might not necessarily continue bringing in bucks for you. You have to spot changes and adapt to them before they bite you.

Relying on AdSense for a passive income stream? Think again.

Blah Blah Blah

If these rumblings seem stupid to you, I’m writing to myself. Anyway, ronburk’s Log-Walking Your Way to >$$$ will certainly be a good read for those enterprising for AdSense.

Categories
Apache Web Server Tutorials & Tips Content / SEO Tips & Tutorials PHP Tips & Tutorials

How to build a php query string without question mark

As a result of the spreading SEO awareness and how Google works, it is always recommended to use as less dynamic URLs as possible for your site. If one must, try using as less variables in the dynamic URL as possible.

A dynamic URL is one with a question mark that passes dynamic variables to the script in the form of http://www.somesite.com/somescript.php?var1=value1&var2=value2. It’s not only ugly but also does no good in terms of search engine optimization. So how can we build php query strings without question mark so that search engines would treat them as static URLs?

Let’s take somesite.com for example. We are going to build a query string that works exactly like http://www.somesite.com/somescript.php?var1=value1&var2=value2 but without the question mark. To make this happen, you need to tamper with .htaccess at the document root of this domain. Add the following rewrite rules:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/somescript.php/(.+)-(.+)_(.+)-(.+)$ /somescript.php?$1=$2&$3=$4

We have used a bit of regular expressions which you can get more information about at here.

With the rewrite rule we have come up with, the apache server then redirects all incoming requests in the form of /somescript.php/a-1_b-2 to /somescript.php?a=1&b=2, successfully rewriting the URLs from dynamic ones to static ones. Then you can use the new rewritten URL on your site and visitors and search engine crawlers would only know about the rewritten ones instead of the real ones.

For more information about how you can play with rewrite rules, visit the apache mod_rewrite document.