Apache

From Wiki

Contents

Apache Tips & Tricks:

To provide a seamless Redirect from http://site/anypage.etc to http://newsite/anypage.etc

The code should go into the specific Virtual Host or apache2.conf (httpd.conf on apache 1.3):

Redirect 301 /site http://newwebsite/site
Redirect 301 / http://newweb/

Got from Skynet config - Thanks Lads.

Redirect from http to https

If you want to force people to use https and/or redirect them seamlessly to https:// without them having to type in the address here is how you can do it with apache using a .htaccess file:

SSLRequireSSL
ErrorDocument 403 https://securewebsite/page.html

Note the above is only a hack. It will break if a similar 403 (which means access is denied) is caught. For example if a "deny from all" is used along with the above, or permissions are set incorrectly - an endless loop will ensue. Im sure there are other ways of seamlessly redirecting http to https, however there is a quick one.

Apache Error Codes

200 OK 
201 Created 
202 Accepted 
203 Non-Authorative Information 
204 No Content 
205 Reset Content 
206 Partial Content 
300 Multiple Choices 
301 Moved Permanently 
302 Moved Temporarily 
303 See Other 
304 Not Modified 
305 Use Proxy 
400 Bad Request 
401 Authorization Required 
402 Payment Required (not used yet) 
403 Forbidden 
404 Not Found 
405 Method Not Allowed 
406 Not Acceptable (encoding) 
407 Proxy Authentication Required  
408 Request Timed Out 
409 Conflicting Request 
410 Gone 
411 Content Length Required 
412 Precondition Failed 
413 Request Entity Too Long 
414 Request URI Too Long 
415 Unsupported Media Type 
500 Internal Server Error 
501 Not Implemented 
502 Bad Gateway  
503 Service Unavailable  
504 Gateway Timeout  
505 HTTP Version Not Supported 

Not a definitive list. Information obtained from: http://bignosebird.com/apache/a5.shtml

Apache REWRITE Module:

Here's a nice one - mod_rewrite which is extremely powerful. I am but using 1 or 2 examples. Write the following examples straight into a .htaccess file :-)

#1. writes all html files as php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
#2. redirects all .html files to newserver.php files
Options +FollowSymlinks
RewriteEngine on
Rewriterule ^(.+)\.html$ http://www.server.com/$1.php [r=301,nc]
#3. This is the best one for reasons I wont divulge. It does need tweaking, but works.
Options +FollowSymlinks
RewriteEngine on
Rewriterule ^(.*)$ http://internal-lan-server/$1 [P]

Display text (footer) under an Apache file listing

Put the following code in a .htaccess file, or in the apache.conf

ReadmeName filetoputatbottom
//or
ReadmeName /path/to/file

The filetoputatbottom may have to be a html page in order to be included correctly. If the above doesnt work, put a .html extension to the file and update the htaccess accordingly. Thats it. You will see somethimg similar to: http://www.burkesys.com/~sburke/apachefooterlisting/ This is very useful for showing a README explaination underneath a listing of files with which to download. Download or install instructions can be placed here as required.

OK. Here's the References:


http://www.widexl.com/scripts/documentation/mod_rewrite.html

http://adstil.indiatimes.com/manual/misc/rewriteguide.html (Apache 1.3 THO)

http://corz.org/serv/tricks/htaccess2.php

http://httpd.apache.org/docs/2.0/mod/mod_autoindex.html

AllowOverride in Apache Configs for .htaccess

Apache's default AllowOverride in www.website.com/pageabc is None. This means that .htaccess files in pageabc do not work. Instead of allowing the complete AllowOverride All, certain pieces and directives can be allowed or disallowed. This is much better, because there is a lot of stuff that can be overriden using the AllowOverride All.

AllowOverride Limit Indexes
//Limit allows deny, allow from etc. etc. Indexes allows control over how indexes are displayed

The full doc is at:

http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride

htaccess Auth using LDAP and htpasswd

1. LDAP Auth Only

LDAP_Debug On
AuthName "Only LDAP domain1 or domain2"
AuthType Basic
AuthOnBind On
LDAP_Server "ldap server ip"
LDAP_Port 389
UID_Attr cn
Sub_DN "OU=Users,ou=staff"
AltSub_DN "OU=Users,ou=undergraduate"
Base_DN "DC=ul,DC=campus"
require user first1.lastname1 first2.lastname2 first3.lastname3

2. Htpasswd Auth Only

Run the following from the cmd line:

htpasswd -bn username pass

Put the above username:DGdmxkU03XUJo into a .htpasswd file and call as follows using a .htaccess:

AuthUserFile /home/user/.htpasswd
AuthName "Only htpasswd users"
AuthType Basic
require valid-user
#or specifically as follows:
require user username

3. LDAP and htpasswd Auth

AuthLDAPAuthoritative Off
AuthUserFile /home/user/.htpasswd
LDAP_Debug On
AuthName "LDAP domain1 or domain2; or htpasswd users."
AuthType Basic
AuthOnBind On
LDAP_Server "ldap server ip"
LDAP_Port 389
UID_Attr cn
Sub_DN "OU=Users,ou=staff"
AltSub_DN "OU=Users,ou=undergraduate"
Base_DN "DC=ul,DC=campus"
require user first1.lastname1 first2.lastname2 first3.lastname3 htpasswduser1 htpasswduser2

htaccess examples complete

http://www.askapache.com/htaccess/ultimate-htaccess-file-sample.html

Multiviews - Automatically append file extension

If you have the following file: /var/www/foo.html and browse to http://localhost/foo foo.html will be shown! The option MultiViews controls this. Typically this option would be in the VirtualHost. It can also be disabled with "Options -MultiViews" in a .htaccess.

This MultiViews caused problems when doing rewrites.

Reference: http://www.gerd-riesselmann.net/archives/2005/04/beware-of-apaches-multiviews

Apache2 Common Configs

How to setup CGI with Apache: http://httpd.apache.org/docs/2.0/howto/cgi.html

Core Directives, Usage and Syntax: http://httpd.apache.org/docs/2.0/mod/core.html


Apache Rewrite via mod_proxy

If you have a webserver running on an internal LAN, and you have a website/access on a external webserver on the LAN, it is possible to proxy requests via the External webserver to inside your LAN. http://192.168.20.20 = Apache on Internal LAN http://193.1.1.10 = Apache acessible Externally and Internally.

mkdir /home/user/public_html/internalweb
vi /home/user/public_html/internalweb/.htaccess
RewriteEngine on
Rewriterule ^(.*)$ http://192.168.20.20/$1 [P]

Then browse to http://193.1.1.10/internalweb, and up comes the website on your Internal LAN.


Links:

http://httpd.apache.org/docs/2.0/mod/mod_auth_ldap.html

http://www.muquit.com/muquit/software/mod_auth_ldap/mod_auth_ldap_apache2.html

http://www.muquit.com/muquit/software/mod_auth_ldap/mod_auth_ldap.html