Apache Server Sides Includes and Htaccess Notes
Apache Server Sides Includes and Htaccess Notes
Apache Server Sides Includes and Htaccess Notes
htaccess Notes
1. How to get rid of referral spam with the .htaccess file. Place the following in your top level .htaccess file and all requests from urls containing the terms in the list will be sent to your 403 page.
RewriteEngine On RewriteBase / SetEnvIfNoCase Referer ".*(lenarcic|casino|more-poker|medportal).*" BadReferrer order deny,allow deny from env=BadReferrer
2. Redirect all of one extension to another extension All requests to file1.txt are redirected to file1.shtml.
RewriteEngine On RewriteBase /myDirectory RewriteRule ^(.*).txt$ $1.shtml [R=301]
3. Redirects with spaces I had an external web site link to one of my pages, but a space was accidentally appended to the link, eg., "<a href="http://www.fincher.org/Misc/Pennies/ ">Pennies</a>". My error.log file was filling up with 404 errors. The easy solution was to do an apache redirect, but it was a little tricky because of the space. I tried putting in the "%20" at the end, but apache liked simple double quotes to define the trailing space.
Redirect "/Misc/Pennies/ " http://www.fincher.org/Misc/Pennies
6. Simple include:
Apache, the most popular web server, allows for "server side" includes. These include one file into another. This is ideal for having a consistent menu bar across files and a standard footer. To use server side includes your file must have the extension ".shtml".
<!--#include file="title.html" -->
For more details visit http://apache.org/docs/mod/mod_include.html 7. To include a file from another directory, the "virtual" keyword is used.
8. <!--#include virtual="../title.html" -->
The result looks like: Last Modified: 11. To execute a cgi script, use the include virtual command:
12. <!--#include virtual="../../cgi-bin/Navigation.pl? Operation=WriteHeadings&MajorSection=$MajorSection/$MinorSection" -->
Where Navigation.pl uses the stone age cgi-lib.pl and looks like,
#!/usr/bin/perl MAIN: { require "cgi-lib.pl" || die "Cannot find cgi-lib.pl: $!"; local(*in,$message,$command); &ReadParse(*in); print &PrintHeader; $Operation = $in{Operation}; $MajorSection = $in{MajorSection}; $MinorSection = $in{MinorSection}; ...
13. Execute a Ruby Script on Dreamhost The interesting thing is to rename the file from a ".rb" extension to a ".cgi" extension
<!--#include virtual="/cgi-bin/hello.cgi"-->
The file "hello.cgi" would look something like this to insert the word "hello..." into the page.
#!/usr/bin/ruby puts "Content-type: text/html\n\n" puts "hello..."
14. Tips on password protection using Apache On the command line use the program "htpasswd" with the "-c" option to create a .htpasswd file the first time. After that do not use the "-c" option. Apache will recreate the file and erase all previous entries. Do not to put .htpasswd in a web-accessable directory where suspicious people could download it and run password programs against it.
% htpasswd -c /big/dom/xsmith/.htpasswd family % htpasswd /big/dom/xsmith/.htpasswd workfriends
In the directory to protect create a file named ".htaccess". Example of a directory named "vacation":
AuthName "vacation" AuthType Basic AuthUserFile /big/dom/xsmith/.htpasswd require user family
<!--#if expr="$ShowAnswers = Yes" --> <li> Changes in the system are hard to implement <li> Risk management is difficult <li> Projects must be well defined at the beginning <li> Cost of fixing errors in requirements increases by 10X at each stage 23. <!--#else --> 24. <li> ___________________________________ 25. <li> ___________________________________ 26. <li> ___________________________________ 27. <li> ___________________________________ 28. 29. <!--#endif -->
which will produce: This document is located at www.fincher.org/tips/web/apache.shtml 32. Getting the QUERY_STRING from the url:
33. <!--#echo var="QUERY_STRING"-->
34. "echo" options By default, echo will encode things like "<" and ">". To prevent encoding of the variable use the "encode='none'" attribute.
<!--#set var="HeaderContents" value="<style type='text/css'> img { padding: 1em;} </style>" --> ...The following is probably in a different file... <!--#if expr="${HeaderContents}" --> <!--#echo encoding="none" var="HeaderContents" --> <!--#endif -->