User-friendly URL's with Gulp-connect

by Filip Lundby.

While developing your project with Gulp, you might want to test URL's without extensions. For example, instead of this:

http://localhost:8080/my-page.html

... you want this:

http://localhost:8080/my-page

Below snippet lets you remove extensions from URL's. You will need to install gulp-connect to make it work.

gulp.task('connect', function() {
  connect.server({
    root: paths.dest,
    livereload: true,
    middleware: function () {
      return [function(req, res, next) {
        // URL rewrite - applies '.html' if url doesnt contain an extension
        if (req.url.length > 1 && !/\/(?:.(?!\/).*\..*)+$/.test(req.url)) {
          req.url = req.url + '.html';
        }
        next();
      }]
    }
  });
});

Cheers!

Leave some feedback