views:

72

answers:

1

I'm trying to create a mod_rewrite rule that will drop any .htm that gets appended to a url that gets put in. For example, if someone types in example.com/faculty/jim.htm, I want drupal to ignore the .htm and just go to the page example.com/faculty/jim. Any mod_rewrite experts out there that can lend a hand? Is there something I can alter with the default drupal rewrite code in .htaccess:

# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Thanks in advance.

+3  A: 

Not tested, but putting

RewriteRule ^(.*)\.htm$ $1

before the standard drupal rewrite rules should do it.

If you want this to trigger only for non existing files, you could add the same conditions as with the main Drupal rewrite rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.htm$ $1

These conditions check if the REQUEST_FILENAME is an existing file or directory.

Henrik Opel
Just as a side note, there's about seven times more questions tagged `mod_rewrite` here than on ServerFault, so [since it's OK](http://meta.stackoverflow.com/questions/39063/mod-rewrite-questions-getting-migrated-to-sf), it actually is probably better to ask here. :)
Tim Stone
@Tim: Thanks for the pointer - I removed the remark (and will try to remember this for future mod_rewrite questions ;)
Henrik Opel
Thanks Henrik, this worked! However, I found a situation on my site where it is referencing an actual .htm file (tinyMCE plugins being the culprit). Is there some way to ignore if a file doesn't exist, but load the file if it does?
bkildow
@bkildow: The answer to that is already available in your original question - I edited my answer accordingly ;)
Henrik Opel
Wonderful, thank you.
bkildow