Never been to CodeSnippets 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 (or not, you can keep them private!)

Apache rewrite rules

Pass all requests for non-existing files or directories to index.php
RewriteCond        %{REQUEST_FILENAME}        !-f                
RewriteCond        %{REQUEST_FILENAME}        !-d
RewriteRule        ^(.*)$                    index.php        [L]

Maintenance mode for apache

Doing some work on your web site but you don't want other people to see what you're doing. Give them a redirect (like the flickr massage screen).
You need to use your own ip of course.

# redirect all visitors to alternate site but retain full access for you
ErrorDocument 403 http://www.alternate-site.com
Order deny,allow
Deny from all
Allow from 99.88.77.66

Latest file to download

Perl script using shell command to get(/grep) the lastest file on directory for download.

Just Create .htaccess (DirectoryIndex .latest.cgi) for more automations, so to get the latest files, just point your download link to: "yoursite.com/files/"

#!/usr/bin/perl
#This is .latest.cgi
$|++; my @file = `ls -1 -t -p | grep -v -P '/'`; 
print "Location: $file[0]\n\n";
exit;

restart Apache process

To reload conf files

To verify changes to .conf files parse OK.
apachectl -t


To issue soft restart (waits for connections to close)
apachectl graceful


Forced restart, dropping connections as necessary.
apachectl restart

Apache - IfModule dir_module directive within httpd-vhosts.conf

Change directory listing / default files served when hitting a directory.

This can be placed directly within the VirtualHost directives.

<IfModule dir_module>
  DirectoryIndex index.html index.php
</IfModule> 

Serve php within .htm

In your .htaccess file (maybe only in a specific folder) add this line to parse a .htm as a php file. This works on TxD accounts.
AddType application/x-httpd-php .htm .php

301 Permanent Redirect rule to consolidate domains

// redirect domain.com to www.domain.com (or vice versa)
// helps substantially with delicious links and other SEO optimization
// see <http://forum.textdrive.com/viewtopic.php?id=5821> for discussion

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

directory listening

.htaccess in home or other directory
Options +Indexes
IndexOptions FancyIndexing NameWidth=*

Apache + mongrel_cluster VirtualHost snippet

Put it in yr httpd.conf, or better yet, an included file. (that thing is monstrous as is)

<VirtualHost *:80>
	ServerName test.com
	DirectoryIndex index.html dispatch.fcgi
	DocumentRoot /domains/test.com/web/public
	RewriteEngine On
	RewriteRule ^(.*/)?.svn/ - [F,L]
	ProxyPass /images !
	ProxyPass /stylesheets !
	ProxyPass /javascripts !
	ProxyPass /examples !
	ProxyPass /map_data !
	ProxyPass /engine_files !
	ProxyPass /javascript !
	ProxyPass /feregions !
	ProxyPass / balancer://test/
	ProxyPassReverse / balancer://test
	ProxyPreserveHost On
	ErrorLog /opt/csw/apache2/var/log/test.com.error
	CustomLog /opt/csw/apache2/var/log/test.com common

	<Proxy balancer://test>
		BalancerMember http://127.0.0.1:8000
		BalancerMember http://127.0.0.1:8001
		BalancerMember http://127.0.0.1:8002
		BalancerMember http://127.0.0.1:8003
		BalancerMember http://127.0.0.1:8004
	</Proxy>
</VirtualHost>

Set up apache2+SSL on FreeBSD

A nice commandline walkthrough. Replace 'eyebeam' with your org name


# setting up apache2 + SSL on FreeBSD
# a list of commands you can copy/paste!
# @author	Jamie Wilkinson <jamie@tramchase.com>
# ganked from <http://www.freebsdmadeeasy.com/tutorials/web-server/apache-ssl-certs.php>

# edit /etc/openssl.conf to set some nice defaults for location, org. name, etc.
# important! change the default dir ./demoCA to /root/sslCA


# setup
cd ~root/
mkdir sslCA
chmod 700 sslCA
cd sslCA
mkdir certs private newcerts
echo 1000 > serial
touch index.txt

# generate certs
openssl req -new -nodes -out eyebeam-req.pem -keyout private/eyebeam-key.pem -config /etc/ssl/openssl.cnf
openssl req -new -x509 -days 3650 -extensions v3_ca -keyout private/cakey.pem -out cacert.pem -config /etc/ssl/openssl.cnf
openssl ca -config /etc/ssl/openssl.cnf -out eyebeam-cert.pem -days 3650 -infiles eyebeam-req.pem 

# keep them somewhere handy
mkdir /etc/ssl/crt
mkdir /etc/ssl/key
cp ~root/sslCA/eyebeam-cert.pem /etc/ssl/crt
cp ~root/sslCA/private/eyebeam-key.pem /etc/ssl/key

# add below to an ssl.conf that you include in your httpd.conf
<VirtualHost *:443>
	ServerName colossus.eyebeam.org:443
	SSLEngine on
	SSLCertificateFile /etc/ssl/crt/eyebeam-cert.pem
	SSLCertificateKeyFile /etc/ssl/key/eyebeam-key.pem
	DocumentRoot /www
	CustomLog /var/log/httpd-ssl-access.log combined
	ErrorLog /var/log/httpd-ssl-error.log
</VirtualHost>