User Tools

Site Tools


Apache Related Quick Help

Compile apache from source

Good known required configuration

| apache 2.x
./configure --prefix=/srv/www --enable-access --enable-autoindex --enable-env --enable-info --enable-mime --enable-proxy --enable-status --enable-usertrack --enable-actions --enable-imap --enable-log_agent --enable-rewrite --enable-alias --enable-auth --enable-cgi --enable-dir --enable-include --enable-log_config --enable-mime_magic --enable-negotiation --enable-setenvif --enable-userdir --enable-define --enable-log_forensic --enable-ssl --enable-so --enable-dav

Dynamic DocumentRoot for Sub-Domains with RewriteEngine/RewriteCond/RewriteRule

<VirtualHost *>
RewriteEngine  on
RewriteCond    %{HTTP_HOST}  ^
RewriteRule    ^(.*)$        /www/$1 [L]
RewriteCond    %{HTTP_HOST}  ^www.*
RewriteRule    ^(.*)$        /www/$1 [L]
RewriteCond    %{HTTP_HOST}  ^(.*)\.yourdomain\.com
RewriteRule    ^(.*)$        /%1/$1 [L]

Broken Image rewrite

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(gif|jpg|png)$ [R=301,L]

mod_auth_mysql installation

/srv/www/apache/bin/apxs -c -L/usr/local/lib/mysql -I/usr/local/include/mysql -lmysqlclient -lm -lz mod_auth_mysql.c

Following Error is showing up when you install it with

mod_auth_mysql.c:591: error: syntax error before “mysql_auth_config_rec”

phpinfo() Based on Location

Alias /php-info /usr/local/apache/libexec/info.php
<Location /php-info>
   AuthUserFile /usr/local/apache/libexec/
    AuthGroupFile /dev/null
    AuthType Basic
    AuthName "PHP Info"
    require valid-user

Location Based Password

<Location /stats/>
AuthUserFile /usr/local/www/statspass
AuthName "Stats Admin"
AuthType Basic
Require valid-user

Disable mod_security for specific folder using .htaccess

| .htaccess
SecFilterEngine Off
SecFilterScanPOST Off

How to run php from cgi-bin folder

Action php-script /cgi-bin/php
AddHandler php-script .php

How to Disable Hotlinking using .htaccess

| .htaccess
RewriteEngine    on
RewriteCond    %{HTTP_REFERER} !^$
RewriteCond    %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?xyz\.com [NC]   ## allow
RewriteCond    %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?pqr\.com [NC]     ## allow
RewriteCond    %{HTTP_REFERER} !^http://([-a-z0-9]+\.)?abc\.org [NC]      ## allow
RewriteRule    \.(gif|jpe?g|png|gif|bmp|jpe|mov|mpg|avi|mpeg|jpg)$ [NC,L]   ## Forward

if you define above .htaccess to all your domain at global position.
you might need to exclude some domain to allow hotlinking to it use following line after second line.

| .htaccess
RewriteCond %{HTTP_HOST} !^www\.nmn\.com$ [NC]

Block IP using .htaccess

| .htaccess
<Limit GET POST>
order allow,deny
allow from all
deny from
deny from
deny from
deny from

How to test mod_perl

$www = $ENV{'SERVER_PORT'} ne "";
eval "use Compress::Zlib"; $hasCompress = $@ ? 0 : 1;
eval "use LWP"; $hasLWP = $@ ? 0 : 1;
eval "use LWP::Simple"; $hasLWPsimple = $@ ? 0 : 1;
if ($hasLWPsimple) {
        eval "use LWP::UserAgent";
        my $ua = LWP::UserAgent->new(timeout => 30);
        $u = "";
        $resp = $ua->request(HTTP::Request->new('GET', $u));
        $okLWP = $resp->content =~ /test\.pl/i;
eval "use CGI::Carp qw(fatalsToBrowser)"; $hasCGI = $@ ? 0 : 1;
$w = 16; $f = "<b>%" . $w . "s</b> : %s\n"; $f2 = " " x $w . " & %s\n";
print "Content-type: text/html\n\n<html><body><pre>" if ($www);
printf $f, "", "v6.01.18";
printf $f, "Perl", "v$]";  
printf $f, "CGI::Carp", ($hasCGI? "" : "not ") . "installed";
printf $f, "Compress::Zlib", ($hasCompress? "" : "not ") . "installed";
printf $f, "LWP", ($hasLWP? "v$LWP::VERSION " : "not ") . "installed";
printf $f, "LWP::Simple", ($hasLWPsimple? "" : "not ") . "installed";
printf $f, "LWP::Simple", "rx " . ($okLWP? "" : "not ") . "ok" if ($hasLWPsimple);
printf $f, "\@INC", $INC[0]; foreach $x (1..$#INC) { printf $f2, $INC[$x];}
foreach $x (sort split(",",
    { printf $f, $x, $ENV{$x} }
print "</pre></body></html>\n" if ($www);

Setting Timezone using .htaccess

you can setup timezone using setting environment using TZ.

| .htaccess
SetEnv TZ Europe/London

Setup Server admin email using .htaccess

To setup default notification email, you can also set based on directory.

| .htaccess
ServerSignature EMail

Block request based on User Agent

There is two way to accomplish this task.

| .htaccess
SetEnvIfNoCase User-Agent "^libwww-perl" bad_bot
SetEnvIfNoCase User-Agent "^EmailWolf" bad_bot
SetEnvIfNoCase User-Agent "^ExtractorPro" bad_bot
SetEnvIfNoCase User-Agent "^CherryPicker" bad_bot
SetEnvIfNoCase User-Agent "^NICErsPRO" bad_bot
SetEnvIfNoCase User-Agent "^Teleport" bad_bot
SetEnvIfNoCase User-Agent "^EmailCollector" bad_bot
SetEnvIfNoCase User-Agent "^Mozilla/4\.0\ \(compatible\)$" bad_bot
Order Allow,Deny
Allow from all
Deny from env=bad_bot


| .htaccess
RewriteEngine On 
RewriteCond %{HTTP_USER_AGENT} ^libwww-perl [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Bot\ mailto:[email protected] [OR] 
RewriteCond %{HTTP_USER_AGENT} ^ChinaClaw [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Custo [OR] 
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/4\.0\ \(compatible\) [OR]
RewriteRule ^.* - [F,L]

How to prevent asking for download files

This will not ask you to download files.

| .htaccess
AddType application/octet-stream .pdf
AddType application/octet-stream .zip
AddType application/octet-stream .mov

Redirect Non-www to www domain

This is useful in may stats.

| .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Redirect www to Non-www

| .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$1 [R=301,L]

Redirect if HTTP_HOST not match

This help you to prevent IP pointing and help gaining ranking.

| .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(.*)\.domain\.com
RewriteRule (.*) [R=301,L]

flv streaming + mp4 streaming with lighttpd proxy

| httpd.conf
ProxyRequests Off
ProxyPass /video
ProxyPassReverse /

In light httpd simply follow the configuration as usual. OR Check link lightttpd

Redirect you can use with apache / lighttpd to content

RewriteEngine On
RewriteRule ^(.*)\.(mp3|mov|mp4)$$1.$2 [NC]

Redirect mobile browser

This will help to redirect all mobile devices to specific page.

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} (mobile|android|blackberry|brew|htc|j2me|lg|midp|mot|motorola|netfront|nokia|obigo|openweb||palm|psp|samsung|sanyo|sch|sonyericsson|symbian|symbos|teleca|up.browser|vodafone|wap|webos|windows.ce) [NC]
RewriteRule ^(.*)$ [R=303,L]

Why 303 : The response to the request can be found under another URI using a GET method. When received in response to a PUT,
it should be assumed that the server has received the data and the redirect should be issued with a separate GET message.

Disable Trace / track

Mostly trace / track is risk to apache server.

RewriteEngine on
RewriteRule .* - [F]
apache.txt · Last modified: 2012/04/20 12:44 by k2patel