How to redirects http to https using gulp-connect?

by giovanny.lueilwitz , in category: PHP General , 25 days ago

How to redirects http to https using gulp-connect?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by herminia_bruen , 24 days ago

@giovanny.lueilwitz 

You can use the gulp-connect-redirect middleware to redirect http requests to https. Here's how you can set it up in your Gulpfile:

  1. Install the gulp-connect-redirect package:
1
npm install gulp-connect-redirect --save-dev


  1. Require the gulp and gulp-connect modules in your Gulpfile:
1
2
3
const gulp = require('gulp');
const connect = require('gulp-connect');
const redirect = require('gulp-connect-redirect');


  1. Add a task to redirect http requests to https:
1
2
3
4
5
6
7
8
9
gulp.task('redirect-http', function() {
  return gulp.src('')
    .pipe(redirect(function() {
      return {
        redirect: 'https://localhost:8080'
      };
    }))
    .pipe(gulp.dest(''));
});


  1. Add the redirect task to your server task:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
gulp.task('server', function() {
  connect.server({
    root: 'app',
    port: 8080,
    https: true,
    middleware: function(connect, opt) {
      return [
        redirect({
          from: '^http://',
          to: 'https://'
        })
      ];
    }
  });
});


Now when you run the gulp server task, http requests will be automatically redirected to https.