Tag Archive for apache

Rewrite underscores to hyphens for SEO URL

Options  FollowSymLinks
RewriteEngine On
RewriteBase /
 
RewriteRule !.(html|php)$ - [S=6]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=underscores:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscores:Yes]
 
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) <a href="http://www.askapache.com/$1" >http://www.askapache.com/$1</a> [R=301,L]

source

Extract IP Addresses From Apache Access Log

cat access.log | grep -o '([0-9]{2,3}.){3}.[0-9]{2,3}' | sort | uniq | wc -l

source

Get Apache Version and other installed software versions as associative array

function getServerSoftware($key = 'SERVER_SOFTWARE')
{
	$regex = "/(?<software>w+)/(?<version>[0-9.a-z]*)/";
		
	if(isset($_SERVER[$key]) && preg_match_all($regex, $_SERVER[$key], $arr))
		return array_combine($arr['software'], $arr['version']);
	else
		return array();
}

//USAGE EXAMPLE

//get array
$serverInfo = getServerInfo();

//print apache version
echo $serverInfo['Apache'];

//print all information
foreach($serverInfo as $software=>$version) echo "{$software}: {$version}<br>
";

source

Get .htaccess Variable Values

<?php
header("Content-Type: text/plain");
$INFO=$MISS=array();
foreach($_SERVER as $v=>$r)
{
  if(substr($v,0,9)=='HTTP_INFO')
  {
    if(!empty($r))$INFO[substr($v,10)]=$r;
    else $MISS[substr($v,10)]=$r;
  }
}
 
/* thanks Mike! */
ksort($INFO);
ksort($MISS);
ksort($_SERVER);
 
echo "Received These Variables:
";
print_r($INFO);
 
echo "Missed These Variables:
";
print_r($MISS);
 
echo "ALL Variables:
";
print_r($_SERVER);
?>

source

.htaccess for permalinks

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

source

PHP SCRIPT PARSING APACHE LOG

<?php
ini_set("memory_limit","64M");
 
$log_file = '/var/log/httpd/...';
$pattern = '/GET (S*news/images/[0-9]{4}S*.php)/';
 
if (is_readable($log_file)) {
 
	$fh = fopen($log_file,'r') or die($php_errormsg);
	$requests = array();
 
	header('Content-Type: text/xml');
	while (!feof($fh)) {
		if ($s = trim(fgets($fh,16384))) {
			if (preg_match($pattern, $s, $matches)) {
				list($whole_match, $request) = $matches;
				$requests[$request]++;
			} 
		}
	}
	fclose($fh) or die($php_errormsg);
 
	// sort the array (in reverse) by number of requests
	arsort($requests);
 
	// print formatted results
	print "<?xml version='1.0' encoding='UTF-8'?>
";
	print "<images>
";
	foreach ($requests as $request => $accesses) {
		printf("	<image path='%s' count='%s' />
", $request, $accesses); 
	}
	print "</images>
";
} else { 
	echo "cannot access logfile!";
}
?>

source

Password protected folder

Put in .htaccess file in the directory you are trying to protect:


AuthType Basic
AuthName "This Area is Password Protected"
AuthUserFile /full/path/to/.htpasswd
Require valid-user

source

Snippet of Log,Logfactory declaration in class

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

    	/**
	 * Logging facilities provided by Apache Commons Logging. When using log4j,
	 * logging facilities are configured in the {@code log4j.properties} file,
	 * which must be in the {@code CLASSPATH}.
	 */
	private static final Log logger =
			LogFactory.getLog(CLASSNAME_HERE.class);

source

Accessing SNIPPLR with Groovy and Apache XML-RPC

import org.apache.xmlrpc.client.XmlRpcClient
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl

def XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl()
config.setServerURL(new URL("<a href="http://snipplr.com/xml-rpc.php"))" >http://snipplr.com/xml-rpc.php"))</a>
def XmlRpcClient client = new XmlRpcClient()
client.setConfig(config)

Object[] params = ["[yourAPIkey]"]
def result = client.execute("user.checkkey",params)
println result

params = ["[yourAPIkey]","groovy"]
result = client.execute("snippet.list",params)
println result

source

Script to create Apache virtual hosts automatically

#!/bin/bash
CMDLN_ARGS="$@" # Command line arguments for this script
export CMDLN_ARGS

# Run this script as root if not already.
chk_root () {

 if [ ! $( id -u ) -eq 0 ]; then
   echo "Please enter your password."
   exec sudo su -c "${0} ${CMDLN_ARGS}" # Call this prog as root
   exit ${?}  # sice we're 'execing' above, we wont reach this exit
              # unless something goes wrong.
 fi

}
chk_root
FILE=$(cat /etc/hosts)
echo $FILE |grep -o "test$";

if [ `echo $FILE |grep -o "$1$"` ];
then
  echo "/etc/hosts file is ok"
else
  echo "127.0.0.1         $1" >> /etc/hosts  #ça c'est ok
fi
cp /etc/apache2/sites-available/timetracker /etc/apache2/sites-available/$1
cat /etc/apache2/sites-available/$1 | sed "s/timetracker/$1/g" |cat > /etc/apache2/sites-available/$1
ln -s /etc/apache2/sites-available/$1 /etc/apache2/sites-enabled/$1
if [ -d /home/jonathan/html/$1 ];
then
  echo "host directory exists";
else
  echo "creating directory"
  mkdir "/home/jonathan/html/$1"
fi
chown jonathan /home/jonathan/html/$1
chgrp jonathan /home/jonathan/html/$1
/etc/init.d/apache2 restart

source