WebFaction
Community site: login faq

Hi, I would like to remove the html extension form my served pages. The server is django and the ui are static html files (using RESTful API to communicate with the server) Tried several ways to do it with .htaccess file, but with no success. Thanks, Guy.

asked 06 Feb '13, 10:47

ContentSimply
111
accept rate: 0%


Jon Skarpeteig over at StackOverflow proposes a possible solution which should be valid for static files.

With .htaccess under apache you can do the redirect like this:

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

As for removing of .html from the url, simply link to the page without .html

<a href="http://www.example.com/page">page</a>

And daverowell over at DjangoSnippets proposes a way to remove .html from all links within django, however my django knowledge is nil so I cannot help with the implementation of said snippet.

1) You've ported an existing web site to Django.
2) The new site URLs don't have html (or htm) extensions.
3) The old site had URLs with html extensions.
4) You want existing bookmarks to work.

Use this middleware to removes trailing .htm and .html extensions from incoming URLs (GETs only) so that your new site honors existing bookmarks. Locate it in settings.MIDDLEWARE_CLASSES near CommonMiddleware because it has similar middleware stack location requirements. If an incoming URL has an html extension, ZapDotHtmlMiddleware strips it out and redirects.

from django import http
import sre

class ZapDotHtmlMiddleware(object):
    """Removes trailing .htm and .html extensions from incoming URLs
    (GETs only) so that a legacy site ported to Django can continue to
    support existing bookmarks. Locate in settings.MIDDLEWARE_CLASSES near
    CommonMiddleware (similar middleware stack location requirements)."""
    # Dave Rowell, Appropriate Solutions, Inc., www.appropriatesolutions.com

    def __init__(self):
        # RE match for .htm or .html at the end of the url, possibly
        # followed by /, but not including it. Compile once, use many.
        self.re_trim_html = sre.compile(r'\.html?(?=/?$)', sre.IGNORECASE)

    def process_request(self, request):
        """ Rewrite incoming URL if it contains an htm or html extension."""
        if request.method == 'GET':
            #Excise any .html ending.
            new_path = self.re_trim_html.sub('', request.path)
            if new_path != request.path:
                # URL was trimmed; redirect.
                # (Borrowed from django.middleware.common.CommonMiddleware.)
                host = http.get_host(request)
                if host:
                    newurl = "%s://%s%s" % (request.is_secure() and 'https' or 'http', host, new_path)
                else:
                    newurl = newpath
                urlencode = request.GET.urlencode()
                if len(urlencode):
                    newurl += '?' + urlencode
                return http.HttpResponsePermanentRedirect(newurl)

        return None
permanent link

answered 06 Feb '13, 12:59

bitspill
31310
accept rate: 0%

edited 06 Feb '13, 13:10

Thanks a lot for the response , but where does the .htaccess should reside on a static file directory?

(06 Feb '13, 13:36) ContentSimply

The .htaccess file would go into the root directory of the static applications directory. Basically the same folder as all the .html files but only the first folder, not any subfolders.

However I believe if you use the django snippet you do not need the .htaccess file.

(06 Feb '13, 23:08) bitspill
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×75
×10

question asked: 06 Feb '13, 10:47

question was seen: 5,119 times

last updated: 07 Feb '13, 00:53

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM