简体   繁体   English

如何重定向离子/网站中的子文件夹

[英]How to redirect subfolders in Ionic/website

I currently have a ionic webapp that is hosted as x url (eg www.myApp.com).我目前有一个 ionic webapp,它作为 x url 托管(例如 www.myApp.com)。 My app has subfolders eg myApp.com/home or myApp.com/details.我的应用有子文件夹,例如 myApp.com/home 或 myApp.com/details。 Unfortunately, whenever I refresh any of these subfolders, I get an error like 'Cannot GET /home' (only the base URL/Domain works).不幸的是,每当我刷新这些子文件夹中的任何一个时,都会收到类似“Cannot GET /home”之类的错误(只有基本 URL/域有效)。

If possible, is there any way to either host these sub-folders on Google Domains (so GD would automatically redirect these links back to the baes myApp.com link if they're refreshed) or do I need to do something within the code itself to accomplish this?如果可能的话,有什么方法可以在 Google Domains 上托管这些子文件夹(所以如果它们被刷新,GD 会自动将这些链接重定向回 baes myApp.com 链接)或者我需要在代码本身中做些什么要做到这一点?

EDIT: I understand now that I have to re-route using my web server so that I can return the index.html when a user refreshes the page on a false link eg (ionicSite.com/Page).编辑:我现在明白我必须使用我的 web 服务器重新路由,以便当用户在错误链接上刷新页面时返回 index.html,例如(ionicSite.com/Page)。 For reference, I've included my server.js file below:作为参考,我在下面包含了我的 server.js 文件:

var express  = require('express');
var app      = express();                               // create our app w/ express
var morgan = require('morgan');             // log requests to the console (express4)
var bodyParser = require('body-parser');    // pull information from HTML POST (express4)
var cors = require('cors');

// Server Redirection changes
const targetURL = "www.ambitia.org";
const path = require ('path');

app.use(morgan('dev'));                                         // log every request to the console
app.use(bodyParser.urlencoded({'extended':'true'}));            // parse application/x-www-form-urlencoded
app.use(bodyParser.json());                                     // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
// app.use(methodOverride());
app.use(cors());

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'DELETE, PUT');
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.use(express.static('www'));
app.set('port', process.env.PORT || 5000);

app.get('/home', (req,res) => {
  console.log('You asked for my home!');
  res.sendFile(path.resolve('/main-es2015.js'));
})

/**
app.get('/*', (req, res) => {
  ('Attempting the redirection goal');
  res.sendFile(path.resolve(__dirname));
}) **/

app.listen(app.get('port'), function () {
  console.log('Express server listening on port ' + app.get('port'));
});

If I understand you correctly, you need to tell your server that it should redirect the requests to your index.html so that Ionic can handle the routing.如果我理解正确,您需要告诉您的服务器它应该将请求重定向到您的index.html以便 Ionic 可以处理路由。

On Apache you can add a .htaccess file in the root folder with the following content:在 Apache 上,您可以在根文件夹中添加.htaccess文件,其内容如下:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

Source: https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations来源: https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM