I am building a reasonable size replacement site in EE and will be migrating content from an existing static-built site.

In order to maintain as many inbound and internal links as possible, what is the best way to add .html to URL's?

Due to the number of URL's involved I don't want to have to redirect each one individually in .htaccess. Using .htaccess to strip .html has been suggested. If this is the best method, what would I need to add to .htaccess to achieve this? Are there any drawbacks or negatives to this method?

  • Did any of the answers help you? If yes, please mark the answer correct by clicking on the checkmark to the left of the answer.
    – Anna_MediaGirl
    Dec 22 '12 at 6:24

I haven't tested this either, but I'm not sure @adrienne's htaccess will work. I'd do it like this:

RewriteEngine on

# 301 redirect path.html to just path
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.html$ $1 [R=301,L]

# the rest of your rewrite rules (e.g. removing index.php) go here
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

To explain the first few lines, first we make sure the requested html file doesn't exist (so we don't break anything), then we use a regular expression to redirect any *.html to * using a 301 (permanent) redirect.

Feel free to edit either of these answers if we're not far off :)

  • Just edited mine to reflect that it needed a \.html on it, thanks. :)
    – adrienne
    Nov 22 '12 at 2:22
  • Hah cool, now we both have the same answer :) Nov 22 '12 at 2:23
  • Thanks folks - I'll give it a go. For some reason I didnt receive any notification that my Q had been answered, hence the delayed response. I'll post back after I've tried it. Nov 27 '12 at 1:23
  • By default you don't get hassled with email notifications. Unless you tick the "email me answers to this question" box when you ask the question. Nov 27 '12 at 2:48

You don't need to redirect them individually in .htaccess; you can do a rewrite to strip .html, as you were told.

Below is a set of directives that should both route whatever.html to /index.php/whatever/ (for EE) and then remove index.php from everything else. This is, however, not tested, so it might require refinement.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)\.html$ /index.php/$1 [R=301,L]

RewriteCond $1 !\.(gif|jpe?g|png|webp|svgz?|tt.|eot|woff|otf|htc|mp.|m4.|og.|flv)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

There shouldn't be any drawbacks or negatives -- it's a tiny amount of extra overhead, but nothing you would ever notice in the scheme of things.

  • I haven't tested either, but I think your first RewriteRule won't actually strip the html, it just redirects to /index.php/path.html Nov 22 '12 at 2:20
  • Testing locally this doesn't seem to do anything. I enter the URL with the .html extension and it stays the same and remains on the index (home) page rather than the destination URL. Doing a regular redirect works fine but obviously will be a bit of a nightmare due to the number of URL's to redirect. Any other ideas? Nov 27 '12 at 1:36
  • Damien, not sure. I can try to work with you on the StackExchange chat when I get back from dinner tonight if you like, and then we can edit the answer?
    – adrienne
    Nov 27 '12 at 1:41
  • 1
    This is the right way to do it. You should not have .html at the end of URL's as it's not "restful". You should persue the htaccess route to remove the .html and make sure that any requests to .html strips it out and redirects it to the right page. Great answer @adrienne ! Nov 30 '12 at 11:55

Have you considered using the Pages module? That's another way of looking at it.

  • Hi Sue and thanks for contributing, could you perhaps expand this answer a little, perhaps with an example as to how this would apply to the OPs use case?
    – Tom Davies
    Nov 30 '12 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.