Hiển thị các bài đăng có nhãn redirect. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn redirect. Hiển thị tất cả bài đăng

301 redirects using Express.js / Node.js

 301 Redirect with Node.js / Express.js - Tutorial with Example (stackfame.com)

const express = require('express');
const app = express();
const port = 3000;
app.get('/old-url/', (req, res, next) => {
res.redirect(301, 'https://new-doamin.com/new-url');
});
app.listen(port, () => {
console.log('Server is running at port: ',port);
});
sadasdasd

and if you want force live request using express middleware use following code:

// forced 301 redirect using express middleware
const express = require('express');
const app = express();
const port = 3000;
app.use((req, res, next) => {
var host = req.get('Host');
if (host === 'www.old-domain.com') {
return res.redirect(301, 'https://new-domain.com/' + req.originalUrl);
}
return next();
});
app.listen(port, () => {
console.log('Server is running at port: ',port);
});

Apache Redirect

https://serverfault.com/questions/344614/simple-apache2-redirect-from-one-domain-to-another

https://www.digitalocean.com/community/questions/how-to-redirect-https-ip-to-https-domain

https://stackoverflow.com/questions/33874212/htaccess-redirect-https-to-another-https

You cannot match scheme http:// in RewriteCond %{HTTP_HOST}.
You can use this rule:
RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=302,L]
You can use the RedirectPermanent directive to redirect the client to your new URL.
Just create a very simple VirtualHost for the old domain in which you redirect it to the new domain:
<VirtualHost *:80>
    ServerName xy.example.com
    RedirectPermanent / http://abc.example.com/
    # optionally add an AccessLog directive for
    # logging the requests and do some statistics
</VirtualHost>

How do I redirect in expressjs while passing some context?

Source: https://stackoverflow.com/questions/19035373/how-do-i-redirect-in-expressjs-while-passing-some-context


There are a few ways of passing data around to different routes. The most correct answer is, of course, query strings. You'll need to ensure that the values are properly encodeURIComponent and decodeURIComponent.
app.get('/category', function(req, res) {
  var string = encodeURIComponent('something that would break');
  res.redirect('/?valid=' + string);
});
You can snag that in your other route by getting the parameters sent by using req.query.
app.get('/', function(req, res) {
  var passedVariable = req.query.valid;
  // Do something with variable
});
For more dynamic way you can use the url core module to generate the query string for you:
const url = require('url');    
app.get('/category', function(req, res) {
    res.redirect(url.format({
       pathname:"/",
       query: {
          "a": 1,
          "b": 2,
          "valid":"your string here"
        }
     }));
 });
So if you want to redirect all req query string variables you can simply do
res.redirect(url.format({
       pathname:"/",
       query:req.query,
     });
 });
And if you are using Node >= 7.x you can also use the querystring core module
const querystring = require('querystring');    
app.get('/category', function(req, res) {
      const query = querystring.stringify({
          "a": 1,
          "b": 2,
          "valid":"your string here"
      });
      res.redirect('/?' + query);
 });
Another way of doing it is by setting something up in the session. You can read how to set it up here, but to set and access variables is something like this:
app.get('/category', function(req, res) {
  req.session.valid = true;
  res.redirect('/');
});
And later on after the redirect...
app.get('/', function(req, res) {
  var passedVariable = req.session.valid;
  req.session.valid = null; // resets session variable
  // Do something
});
There is also the option of using an old feature of Express, req.flash. Doing so in newer versions of Express will require you to use another library. Essentially it allows you to set up variables that will show up and reset the next time you go to a page. It's handy for showing errors to users, but again it's been removed by default. EDIT: Found a library that adds this functionality.
Hopefully that will give you a general idea how to pass information around in an Express application.

Renewing Facebook Graph API token automatically?

  Mã truy cập dài hạn https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-long-lived/ https://community.n8n.io/t/re...