Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8434903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:47:06+00:00 2026-06-10T06:47:06+00:00

What is the best way to do this? I’d like to redirect all requests

  • 0

What is the best way to do this?

I’d like to redirect all requests from www.example.com to example.com

*.example.com to example.com would be ideal

I would think some type of middleware. I still hate this, because it seems so inelegant and slightly wasteful, but I think my only option is to do this server-side.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:47:08+00:00Added an answer on June 10, 2026 at 6:47 am

    Since Express 3 doesn’t use its own HTTP server (instead you pass your app to http.createServer), it doesn’t know what port it’s running on unless you tell it. That said, you can do basically what you want to do with the following:

    app.use(function(request, response, next) {
      var newHost = request.host.replace(/^www\./, '');
      if (request.host != newHost) {
        // 301 is a "Moved Permanently" redirect.
        response.redirect(301, request.protocol + "://" + newHost + request.url);
      } else {
        next();
      }
    });
    

    You could export this in a module and wrap it in a generator that takes a port:

    // no_www.js
    
    module.exports = function(port) {
      app.use(function(request, response, next) {
        var newHost = request.host.replace(/^www\./, '');
        if (request.host != newHost) {
          var portStr = '';
          if (request.protocol == 'http' && port != 80) portStr = ':' + port;
          if (request.protocol == 'https' && port != 443) portSt r= ':' + port;
          // 301 is a "Moved Permanently" redirect.
          response.redirect(301, request.protocol + "://" + newHost + portStr + request.url);
        } else {
          next();
        }
      });
    }
    
    // app.js
    
    var noWww = require('./no_www');
    var app = express();
    
    app.configure("development", function() {
      app.set("port", 3000);
    });
    
    ...
    
    app.use(noWww(app.get('port')));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be the best way to design this MySQL database? I have cars
I am curious which is the best way to convert time like this 2008-04-23
I would like to know which is the best way to organize the dll
i would like to know what is the standard/best way of doing the following:
This question: Best way to return status flag and message from a method in
What would be the best way to do this, without violating the design principles?
What is the best way to make buttons(javascript) like this and what is the
I like the way that this site is laid out http://www.templerinteriors.co.nz/what-weve-done/kitchens/kitchen-gallery-c/ but what I
The best way of describing this is I have a table of people with
Is the best way to do this with Regex? I don't want it picking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.