« Alphabetize Your Entries | Main | What is .htaccess? »

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.

About .htaccess

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:

hidden_after.gif


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):

Options -Indexes
<IfModule mod_rewrite.c>
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.htm default.htm default.html default.asp /mtview.php
</IfModule>
RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ /mtview.php [L,QSA]
</IfModule>
<IfModule !mod_rewrite.c> 
ErrorDocument 404 /mtview.php
ErrorDocument 403 /mtview.php
</IfModule>

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.

Options -Indexes

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:

indexes_view.gif


Your web host needs to have two Apache modules - mod_rewrite and mode_dir.

<IfModule mod_rewrite.c> 
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.htm default.htm default.html default.asp /mtview.php
</IfModule>

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.

RewriteEngine on


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.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f


This next line tells the server that all other requests go to mtview.php. </IfModule> is a closing tag for the rewrite module.

RewriteRule ^(.*)$ /mtview.php [L,QSA]
</IfModule>


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.

<IfModule !mod_rewrite.c>
  ErrorDocument 404 /mtview.php
  ErrorDocument 403 /mtview.php
</IfModule>

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:

Options -Indexes
DirectoryIndex index.php index.html index.htm default.htm default.html default.asp /mtview.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /mtview.php [L,QSA]


Your .htaccess file should look like this:

htaccess_graphic_file.gif

Note that the DirectoryIndex line that ends with mtview.php is all on one line.


Finishing Touches

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

Options -Indexes
DirectoryIndex index.php index.html index.htm default.htm default.html default.asp /subdirectory/mtview.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /subdirectory/mtview.php [L,QSA]


Potential Conflicts

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!

Comments (3)

We are trying to get our 3.2 install to work with dynamic publishing. The .htaccess file doesn't seem to get dynamically created - and when we add the code to the .htaccess file manually and put it there ourselves - the root index.php doesn't work and the archives.php - is not found - and then it points to the .static version.

I'm sure this is all a config issue on our end - but there doesn't seem to be anyway to get the publishing to work this way for us yet.

Hi Jay -

Unfortunately, the best answer might be to upgrade. I can't say for certain, but it's quite possible that there's a problem with the .htaccess file being generated by your 3.2 install - and it's unlikely that you'll get much support out of anyone for it. More importantly, if you do, you'll likely spend a lot more time and effort trying to get it to work than you will getting 3.35 or even MT4 up and running, when it's possible that you could just be fighting a bug in the software.

I know that getting dynamic publishing up and running in the earliest versions of the software was a lot harder - but that's certainly one option open to you. Other than that, you may want to look at this article. It's a bit dated, but it goes through the options fairly well. I hope it helps!

meeero:

thank you for that tutorial, it helped me quite a lot!

Post a comment

(If you haven't left a comment here before, your comment may need to be approved before will appear on the entry. Thanks for waiting.)