簡體   English   中英

如何重定向離子/網站中的子文件夾

[英]How to redirect subfolders in Ionic/website

我目前有一個 ionic webapp,它作為 x url 托管(例如 www.myApp.com)。 我的應用有子文件夾,例如 myApp.com/home 或 myApp.com/details。 不幸的是,每當我刷新這些子文件夾中的任何一個時,都會收到類似“Cannot GET /home”之類的錯誤(只有基本 URL/域有效)。

如果可能的話,有什么方法可以在 Google Domains 上托管這些子文件夾(所以如果它們被刷新,GD 會自動將這些鏈接重定向回 baes myApp.com 鏈接)或者我需要在代碼本身中做些什么要做到這一點?

編輯:我現在明白我必須使用我的 web 服務器重新路由,以便當用戶在錯誤鏈接上刷新頁面時返回 index.html,例如(ionicSite.com/Page)。 作為參考,我在下面包含了我的 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'));
});

如果我理解正確,您需要告訴您的服務器它應該將請求重定向到您的index.html以便 Ionic 可以處理路由。

在 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>

來源: 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