Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 21-30 of 49 total

.htaccess That Fixes The Trailing Slash Error

// This .htaccess file is for use with a Rails application that is accessed by a symbolic link. This fixes an error in which the directory URL must have a trailing slash. Otherwise, the user receives a 400 Bad Request error.

Options +FollowSymLinks +ExecCGI

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME}    -d
RewriteCond %{SCRIPT_FILENAME}      ^.*[^\/]$
RewriteRule ^(.*)$ $1/ [N]

#Put the directory your Rails app is in here.
RewriteBase /directory

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.cgi?$1 [QSA,L]

.htaccess error404.php

// This goes in .htaccess in the web root.

# error docs
ErrorDocument 404 /error404.php


// Use existing site template to start 404 page then add this code in your main content div.

<h1>Page not found</h1>
<?php
/* Error 404 code for ***** website */

$webmaster = "webmaster@camrider.com";
$host     = getenv("REMOTE_HOST");
$referrer = getenv("HTTP_REFERER"); 
$path     = getenv("REQUEST_URI"); 

// time in this format: 13/Nov/2000:10:50:38
$time = strftime("%d/%b/%Y:%T");

if ($referrer == "") {
  $referrer = "";
} else {
  $referrer = "<p>You came to this page from $referrer, this could be a broken link so please <a href=\"mailto:$webmaster?subject=Error 404 on $path from $referrer\">email the webmaster</a> to inform us of this.</p>";
}


?>
<h2>Sorry, we couldn't find the page <?php echo $path ?> on this website</h2>
<?php echo $referrer; ?>
<p>Please use the navigation links to help locate what you're looking for.</p>

replace file extension with apache rewrite

// in this example *.html is replaced by *.php
// its a redirect [R] and it is is permanent (code is 301)

RewriteEngine On
RewriteBase   /the_directory

RewriteRule  ^(.*).html$ $1.php [R=301]

rewrite rules

// if cms has cryptic urls
// in apache url-rewriting with .htaccess can help

RewriteRule cms/fest$ cms/index.php?option=content&task=view&id=176&Itemid=202 [NC]


// [NC] = not case-sensitive

Exempt directories from Apache's ProxyPass directive

Very useful if forwarding to Mongrel, etc.

        ProxyPass /stylesheets !
        ProxyPass /javascripts !
        ProxyPass /images !     
        ProxyPass / http://127.0.0.1:3010/
        ProxyPassReverse / http://127.0.0.1:3010/  

A tool to split log files by hostname

This script can be used to split a log file by the hostname in the request. It is designed for use with lighttpd virtual hosting, to prepare the log for Webalizer. It should be run with a cron job and CLI PHP.

<?php
// Copyright (C) 2006 Craig Spurrier
// Released under the terms of the MIT/expat license.

$serverip = "127.0.0.1"; //Set to your IP address or default hostname, so requests without hostnames can be sorted 
$log = file_get_contents("/var/log/lighttpd/access.log"); //Log location
$fh = fopen("/var/log/lighttpd/access.log", 'w'); //Clear the log file
fclose($fh);
$lines = explode ("\n", $log);
foreach ($lines as $line){
$parts = explode (" ", $line);
$hostname = strtolower($parts[1]);
if (substr($hostname, 0, 4) == 'www.'){$hostname = substr($hostname, 4);} //Treat www. as the same as the www-less version
if ($hostname == '-'){$hostname = $serverip;} //Set a hostname for request without hostnames
$fh = fopen("/var/log/lighttpd/users/$hostname.access.log", 'a'); //Add the entry to the log for that hostname. Make sure this script can write to this location.
fwrite($fh, "$line\n");
fclose($fh);
}
?>

Add www in front of domain name

Add 'www' in front of the domain name through .htaccess file.

For example http://yoursite.com will automatically redirect to http://www.yoursite.com

RewriteEngine On
RewriteCond %{HTTP_HOST} ^yoursite\.com
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [R=permanent,L]

Basic ant script with vim & jikes

Apache ant build XML. This will use jikes in place of javac. Any compiler error output is formatted so that vim can parse it.

ant is a modern alternative to make. The build script is an XML file. It works particularly well with java. Download it for free from the Apache ant site. Most people find ant a lot nicer to live with than make.

<?xml version="1.0"?>

<project name="Hello" default="compile" basedir=".">
  <property name="name" value="Hello"/>
  <property name="version" value="1.0"/>

  <!-- Project directories.
  -->

  <property name="build" value="build"/>
  <property name="dist" value="dist"/>
  <property name="src" value="src"/>

  <!-- Compiler directives.
  -->

  <property name="optimize" value="off"/>
  <property name="deprecation" value="on"/>
  <property name="debug" value="on"/>

  <property name="build.compiler" value="jikes"/>
  <property name="build.compiler.emacs" value="true"/>

  <target name="init">
    <tstamp/>
    <mkdir dir="${build}"/>
  </target>

  <!-- Compile all the .java files from the source directory into
       the build directory.
  -->

  <target name="compile" depends="init">
    <javac srcdir="${src}" destdir="${build}" includes="**/*.java"
      debug="${debug}" deprecation="${deprecation}" optimize="${optimize}">
    </javac>
  </target>

  <target name="clean" depends="init">
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>

  <target name="test" depends="compile">
    <java classname="HelloWorld" fork="yes">
      <classpath>
        <pathelement location="${build}"/>
      </classpath>
    </java>
  </target>
</project>

securing the /home directory

I'm still working on getting this one perfect.

chmod 701 /home/*
chmod 705 /home/*/public_html
chmod 604 /home/*/public_html/*.*

slashdot protection

Replace aaa.bbb.ccc.ddd with your own IP (so you always get to your site).
Replace www.yourdomain.com with your actual domain.

Just don’t mess with the user_agent and query_string lines. Those ensure that the Coral servers themselves can retrieve your page when they need to.

(Shamelessly stolen from http://ottodestruct.com/diggprotectionrules.txt)

RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^aaa.bbb.ccc.ddd$
RewriteCond %{HTTP_USER_AGENT} !^CoralWebPrx
RewriteCond %{QUERY_STRING} !(^|&)coral-no-serve$
RewriteCond %{HTTP_REFERER} ^http://(www\.)?digg\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?slashdot\.org [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?slashdot\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?fark\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?somethingawful\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?kuro5hin\.org [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?engadget\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?boingboing\.net [OR]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?del\.icio\.us
RewriteRule ^(.*)$ http://www.yourdomain.com.nyud.net:8080$1 [R,L]
« Newer Snippets
Older Snippets »
Showing 21-30 of 49 total