Things look quiet here. But I've been doing a lot of blogging at
dan.langille.org because I prefer WordPress now.
Not all my posts there are FreeBSD related.
I am in the midst of migrating The FreeBSD Diary over to WordPress
(and you can read about that here).
Once the migration is completed, I'll move the FreeBSD posts into the
new FreeBSD Diary website.
This article shows you how you can rewrite URLs as they arrive at your
Apache server. I used this on my website. I moved everything from /freebsd/ to
/ and renamed all the files from *.htm to *.html. I told Apache how to process the
incoming URLs so the correct files were found. This is way-cool stuff!
although I was doing the rewrites for quite some time after reorganizing the website, I
have now removed the rewrites.
This solution requires mod_rewrite (which is included in the Apache port I used). Make sure the following
are present in your httpd.conf:
The above translates any request for an .xyz file to a .html
file. As I had renamed all such files, this is enough for me. It also changes
the URL in the user's browsers. If they had requested foo.xyz,
their browser will display foo.html. If foo.html
doesn't exist, they will get the normal error screen.
The "=permanent" indicates to the client that this is a
permanent change in the URL. If you don't supply this option, the relocation is
I have seen a solution which first checks if foo.html exists, and if
it does, return foo.html. If foo.html does not
exist, the URL in the browser remains unchanged at foo.htm. And I
wrote it about it here.
NOTE: In recent testing, I was unable to get this solution to work.
This solution deals with the moving of files from one directory to another
as well as the renaming of the extension.. Here is what I added to my the virtual host section of /usr/local/etc/apache/httpd.conf:
These solutions can also be accomplished with .htaccess entries.
This is useful to know if you are not running your own webserver and do not have access to
httpd.conf. Here's what I put into my .htaccess for this
RewriteRule ^(.*)\.htm$ $1.html [R=permanent]
The virtual host in question must allow FileInfo to be overridden.
This set of rules is based on an example from http://www.engelschall.com/pw/apache/rewriteguide/#ToC21
and can be used when you have renamed files from .htm to .html.
It first checks to see if a file with the new extension exists. If it does,
it returns that URL. Otherwise, it returns the original URL.
If you have renamed a file, and wish to redirect incoming requests, you can do this:
RewriteRule ^about\.htm$ about.html [R=permanent]
The above will result in requests for about.htm being redirected to about.html.
The "^" represents the start of the substitution. The "\"
is an escape which allows the "." The "$" represents the end of
Redirects vs rewrites
When should you use a redirect? When should you use a rewrite?
If the file is on the same website, you should use a rewrite. If the file is
on another server, you should use a redirect. Why? A simple answer is
bandwidth. A redirect sends the new URL back to the client and the client must
reissue the URL request, which creates more traffic. With a rewrite, the original
request is satisfied and a new URL is returned along with the new file. The client
does not have to reissue anything.
What I'm using now
NOTE: Since writing this article, I have removed these rewrites from my
When I rearranged the Diary, I moved everything from /freebsd/
into /. I wanted the old URLs to still work. The following is the
contents of /freebsd/.htaccess: