Dynamic Pages & The .htaccess

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 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 as 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 publichtml directory of your server. When placed in the root publichtml directory, the .htaccess file contents affect every file in your public directory, including every file in every sub directory. For example publichtml/.htaccess would affect files in publichtml as well as files in publichtml/files/ and publichtml/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:

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>

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:

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

Your web host needs to have two Apache modules - modrewrite and modedir. Modrewrite invokes dynamic pages. Moddir.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. 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.

RewriteEngine on

This line starts the mod_rewrite module

RewriteCond %{REQUEST_FILENAME} !-d

If the request is for a real directory (one that exists on the server), mtview.php isn't served.

RewriteCond %{REQUEST_FILENAME} !-f

If the request is for a file that exists already on the server, mtview.php isn't served.

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

All other requests are sent to mtview.php. </IfModule> is a closing tag for the rewrite module.

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

If mod_rewrite isn't available, these lines tell the server 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 this 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]

It should look like this:

The htaccess file

Finishing Touches

The code shown here must start on a new line within the .htaccess file. But apart from that there are no other rules on where it 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.

I have a .htaccess and a mtview.php file in publichtml/ 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 publichtml .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.

8 Comments

abhi said:
on Sep 19, 2004 9:39 AM | Reply

i would like to mention a few things here:

first, in most web servers, the options -Indexes is disabled so the first line of your code is not really a necessity. besides the dynamic pages really does not exist on the webserver, so even if the folder is indexed (some might wanna deleberately do it) it would show nothing.

second, the errordocuments are forced to open mt-view.php. what happens if there was a previous similar command for a different 404 or 403 page... it would most probably give a 500 error... so it would be nice to mention tat as well.

other than this, its a wonderful tutorial n a definite well done job.

Arvind Satyanarayan said:
on Sep 19, 2004 11:07 AM | Reply

The errordocument conflict has been mentioned in "Potential Conflict"

Also the -indexes option is there for dynamic pages with a trailing slash as the permalink.

Soon Hyouk Lee said:
on Sep 9, 2005 12:06 PM | Reply

I've tried to get dynamic pages to load, but no avail. When I go to the above url, I get the static pages...that is, if I edit the blog and republish, the above url doesn't change.

Pointing to

soon.gotdns.org/mt-static/clicks/mtview.php gives a blank white screen.

Any ideas on what is happening? I have been looking through all the documentation - PHP works, redirect appears to work, but something isn't quite right. It's a windows apache install.

Soon Hyouk Lee said:
on Sep 18, 2005 4:48 AM | Reply

Nevermind, I figured it out - it was a PHP error on my end.

The above comments can be removed...

Thanks!

fermuar said:
on Jan 18, 2006 11:42 PM | Reply

if there was a previous similar command for a different 404 or 403 page... it would most probably give a 500 error... so it would be nice to mention tat as well.

akam said:
on Mar 3, 2007 1:08 AM | Reply

Hello, I want to disable PHP in one folder, how can I do it by .htacces??

at the same time I wont to allow Uploding .htaccess in this folder?

Mudasar Khan Niazi said:
on Mar 29, 2007 11:45 AM | Reply

It is very informative page that has a lot of information about .htaccess file.

Nioton said:
on Apr 30, 2007 3:03 AM | Reply

thanks, but how can we prevent out ADDON domains to not inherit the settings from the root folder which they are in?

Leave a comment

Preview