當您在angularjs中啓用了html5 mode/push state mode ON時,這是您的服務器應該處理的常見情況。如果我正確理解了您的問題,那麼當您刷新頁面時,服務器將返回404,否則,如果從着陸頁導航,該頁面會呈現罰款。讓我知道如果是這樣的情景:
- 角應用的土地進入主屏幕,說您的網域/
- 導航至其他頁面 - 您的網域/ SomePage的(這將是您的網域/#SomePage的在散列爆炸模式的情況下)
- 刷新頁面 - >拋出404
如果您面對的情況是上述給定的一樣,則t他是發生了什麼:
- 加載主頁 - >角被bootstraped和路由設置
- 導航到「SomePage的」 - >角航線處理這一點,並顯示「SomePage的」
- 刷新頁面 - >請求到達服務器並請求您的網域/ SomePage的 - 這是不是在服務器可用
- 服務器返回404
如何來處理呢? 在404的情況下,是否將URL從服務器重寫回your_domain/。這將引導角應用程序和角路線將處理該請求在這裏
更多細節 - https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode
複製粘貼從上述網站
阿帕奇重複寫
<VirtualHost *:80>
ServerName my-app
DocumentRoot /path/to/app
<Directory /path/to/app>
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule^- [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule^index.html [L]
</Directory>
</VirtualHost>
Nginx的重寫
server {
server_name my-app;
root /path/to/app;
location/{
try_files $uri $uri/ /index.html;
}
}
Azure的IIS重寫
<system.webServer>
<rewrite>
<rules>
<rule name="Main Rule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
快速重複寫
var express = require('express');
var app = express();
app.use('/js', express.static(__dirname + '/js'));
app.use('/dist', express.static(__dirname + '/../dist'));
app.use('/css', express.static(__dirname + '/css'));
app.use('/partials', express.static(__dirname + '/partials'));
app.all('/*', function(req, res, next) {
// Just send the index.html for other files to support HTML5Mode
res.sendFile('index.html', { root: __dirname });
});
app.listen(3006); //the port you want to use
ASP.Net C#重複寫
在Global.asax中
private const string ROOT_DOCUMENT = "/default.aspx";
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string url = Request.Url.LocalPath;
if (!System.IO.File.Exists(Context.Server.MapPath(url)))
Context.RewritePath(ROOT_DOCUMENT);
}
我認爲這是因爲你使用'$ locationProvider.html5Mode(true);'。試試這個'$ locationProvider.html5Mode(false);' – Miqe
Miqe你是一位救生員,非常感謝你!但是現在,當我更改頁面時,瀏覽器地址欄中的網址不會更改。我怎樣才能解決這個小問題? – Daigo
很高興知道,我已將我的評論作爲答案。 – Miqe