September 17, 2004
Htaccess and Dynamic Publishing
Co-authored by Arvind Satyanarayan and Elise Bauer
Tutorial cross posted on Movalog and Learning Movable Type
With the release of Movable Type 3.1 comes a new and powerful feature - Dynamic Publishing. To take advantage of Dynamic Publishing, you need to edit or create a file on your Apache server called .htaccess as explained in the Movable Type Manual. htaccess files can give you extra control over your server, allowing you to password protect directories, enable server side includes, generate custom error messages, and block users by IP address among other things. (See this Guide to .htaccess for more information.)
Note: You should really know what you are doing before attempting to work on an htaccess file. It is a powerful and potentially dangerous file. In some extreme cases you could lock yourself out of your domain completely, including the use of back-end tools such as cPanel and FTP. (Elise once took her site offline for several hours unintentionally after changing the code on a .htaccess file. Fortunately her web host tech support came up with the proper code that got the file and the site working again.) If you are unsure, check your .htaccess code with your web host before uploading. You might want to experiment with a .htaccess file in a subdirectory first, to avoid causing site-wide problems. We take no responsibility for what may result on your server by following these instructions.
An htaccess file is a simple ascii text file which you create or edit in a text editor. The dot (.) before the word htaccess indicates that .htaccess is a file extension. Some text editors will add on an additional .txt file extension when you save your document. When you upload it to your server however, make sure the final file is named .htaccess and set the file's permissions to 644. The (.) in .htaccess also makes the file "hidden" on a unix server. Some FTP clients do not show the .htaccess because of its hidden status. For such FTP clients you will need to specify the "-a" parameter which displays all files including those that are hidden. For example:
Some web hosts do not allow use of .htaccess files, so check with your web host to make sure that it does before attempting to upload one. According to this Guide to .htaccess, "Most commands in htaccess are meant to be placed on one line only, so if you use a text editor that uses word-wrap, make sure it is disabled or it might throw in a few characters that annoy Apache to no end, although Apache is typically very forgiving of malformed content in an htaccess file."
The .htaccess file usually resides in the root public_html directory of your server. When placed in the root public_html directory, the .htaccess file contents affect every file in your public directory, including every file in every sub directory. For example public_html/.htaccess would affect files in public_html as well as files in public_html/files/ and public_html/files/morefiles. You can override the effects of the root public_html .htaccess file in a subdirectory by placing another .htaccess file in that subdirectory.
The Dynamic Publishing .htaccess Code
To enable Dynamic Publishing, the MT Manual lists this code (minus the commented-out lines) for the .htaccess file (part of the code is running off of the page, see the original in the MT manual):
Note: The line that starts with "DirectoryIndex" ends with "mtview.php". This line needs to be all on one line in your .htaccess file.
Let's review each line to see what it does.
This first line disables something called indexes and is important for security purposes. Indexes allow the public to see files and folders on your server, for example:
Your web host needs to have two Apache modules - mod_rewrite and mode_dir.
Mod_rewrite invokes dynamic pages. Mod_dir.c provides for "trailing slash" redirects and serving directory index files. (See Apache documentation). The third line in the code above is a common list of directory index options. Note that although this line is broken up into two lines so they can fit on the page in this tutorial, this line (the one that begins with DirectoryIndex and ends with mtview.php) needs to be all in one line in your .htaccess file. If a URL has a trailing slash, for example if it is a directory like http://www.movalog.com/archives/ , then the server checks for files that are the most common main pages of the directory, including index.html, index.php, etc. If such a file is found, that file is served. Otherwise mtview.php takes over. Notice that at the end of the list is /mtview.php. It is invoked if the other choices aren't present.
The next line starts the mod_rewrite module.
These two lines tells the server not to serve up mtview.php if the request is for a real directory or for a real file.
This next line tells the server that all other requests go to mtview.php. </IfModule> is a closing tag for the rewrite module.
These lines tell the server that if mod_rewrite isn't available, to route the requests to mtview.php. It works by sending any requests for non-existent files, or files that are forbidden, to mtview.php and lets mtview.php handle it.
If you know your host supports mod_rewrite, you don't need the above last bit of code. In this case you can trim your .htaccess code down to:
Your .htaccess file should look like this:
Note that the DirectoryIndex line that ends with mtview.php is all on one line.
You will either create a new .htaccess file in a text editor with the code shown here, or add this code to an existing .htaccess file. If you add the code to an existing .htaccess file, the code must start on a new line within the file. Apart from that there are no other rules on where the code should go within the file. You will need a set of .htaccess and mtview.php files for every weblog directory in which you plan to use dynamic publishing. It is not enough to have one .htaccess file and one mtview.php file in the public_html directory to work for all of your weblogs.
Arvind has a .htaccess and a mtview.php file in public_html/ that is used for his main Movalog weblog. His sideblog resides in a subdirectory, and for this he has created new .htaccess and mtview.php files for that subdirectory. The .htaccess and the mtview.php files inside of the sideblog directory take priority over the public_html .htaccess and mtview.php files for that sideblog.
If you have a similar situation, and are creating a .htaccess for an additional weblog within a subdirectory of your public_html directory, you will need to change the code in the .htaccess file you load to that subdirectory indicating the subdirectory location of the mtview.php file. For example
If you are amending an existing .htaccess file with the code shown here you should check for potential conflicts. The main conflict that may arise is if you have any existing definitions for custom error templates (usually this code begins with ErrorDocument). If you have existing ErrorDocument code you can remove it and use the Dynamic Pages Error Template for your custom error pages. Note that MT will serve up only one error template, so if you are used to having several, with different messages, you will need to combine the messages into that one template. On the Dynamic Pages Error Template swap out the <$MTErrorMessage$> tag for your custom error message.
Note that if you already have a .htaccess file in your root public directory, a subdirectory .htaccess file will only affect the files and directories in the subdirectory. That extends to error messages as well.
If you are unsure about potential .htaccess conflicts with an existing file, it is safest to check with your webhost first, before implementing an additional file.
Many thanks to Brad Choate for vetting this tutorial before we posted it. Thanks Brad!
Has this tutorial been helpful? Please consider linking to Learning Movable Type at http://learningmovabletype.com/ . Thanks!
If you would like to send a trackback
please use the following URL: http://learningmovabletype.com/cgi-bin/mt32/mt-tb.cgi/342
Wow, woke up today and many things were happening. The first of which is my name being on the Movable Type front page !! For the past couple of days, Elise and I had worked hard on a tutorial to......[read more]
Tracked: September 18, 2004 10:30 AM» MT dynamic publishing notes (yet again) from ***Dave Does the Blog
Okay, here's the good news: I figured out what was wrong with the dynamic publishing options I had set for......[read more]
Tracked: October 4, 2004 01:00 PM