AngularJS框架提供了一种HTML5模式的路由,可以直接去掉#号。通过设置$locationProvider.html5Mode(true)就行了。
1.添加base标签
<html lang="zh-CN" ng-app="app"> <head> <base href="/"> //增加base标签 // 省略代码 head>
2.编辑app.js,增加 $locationProvider.html5Mode(true);
app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) { $stateProvider ...//省略代码 $locationProvider.html5Mode(true); }]);
3.讲html页面中跳转部分的 ' #/ ' 去掉
4.WebServer地址转发
这里需要使用到UrlRewriteFilter这个插件,使用方法: 1.将urlrewritefilter-4.0.3.jar包放入应用目录“WEB-INF/lib”下。 2.在WEB-INF/web.xml配置文件中加入:
UrlRewriteFilter
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter UrlRewriteFilter /* REQUEST FORWARD
3.在WEB-INF目录新建“urlrewrite.xml”转发规则文件。内容如下:
<urlrewrite>
<rule> <from>^/[a-zA-Z]+(/([a-zA-Z]|[0-9])*)*$from> <to>/index.htmlto> rule> urlrewrite>
其中rule部分匹配地址用到了正则表达式,这里不赘述。这样就将规则设置完成了,重启tomcat刷新页面,没有#也不会404了。
Nginx用到的是try_files,修改nginx的配置文件,增加try_files配置。
server {
server_name localhost;
root /www;
location / {
try_files $uri $uri/ /index.html; } }
其中root的目录,对应ng-app的定义文件html的目录
ServerName localhost
DocumentRoot /www
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]
<system.webServer>
<rewrite>
<rules> <rule name="Main Rule" stopProcessing="true"> <match url=".*" >match> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" >add> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" >add> conditions> <action type="Rewrite" url="/" >action> rule> rules> rewrite> system.webServer>
var express = require('express');
var app = express();
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(8080); //the port you want to use
3.WebServer地址转发
这里需要使用到UrlRewriteFilter这个插件,使用方法: 1.将urlrewritefilter-4.0.3.jar包放入应用目录“WEB-INF/lib”下。 2.在WEB-INF/web.xml配置文件中加入:
UrlRewriteFilter
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter UrlRewriteFilter /* REQUEST FORWARD
3.在WEB-INF目录新建“urlrewrite.xml”转发规则文件。内容如下:
<urlrewrite>
<rule> <from>^/[a-zA-Z]+(/([a-zA-Z]|[0-9])*)*$from> <to>/index.htmlto> rule> urlrewrite>
其中rule部分匹配地址用到了正则表达式,这里不赘述。这样就将规则设置完成了,重启tomcat刷新页面,没有#也不会404了。
Nginx用到的是try_files,修改nginx的配置文件,增加try_files配置。
server {
server_name localhost;
root /www;
location / {
try_files $uri $uri/ /index.html; } }
其中root的目录,对应ng-app的定义文件html的目录
ServerName localhost
DocumentRoot /www
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]
<system.webServer>
<rewrite>
<rules> <rule name="Main Rule" stopProcessing="true"> <match url=".*" >match> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" >add> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" >add> conditions> <action type="Rewrite" url="/" >action> rule> rules> rewrite> system.webServer>
var express = require('express');
var app = express(); 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(8080); //the port you want to use
3.WebServer地址转发
这里需要使用到UrlRewriteFilter这个插件,使用方法: 1.将urlrewritefilter-4.0.3.jar包放入应用目录“WEB-INF/lib”下。 2.在WEB-INF/web.xml配置文件中加入:
UrlRewriteFilter
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter UrlRewriteFilter /* REQUEST FORWARD
3.在WEB-INF目录新建“urlrewrite.xml”转发规则文件。内容如下:
<urlrewrite>
<rule> <from>^/[a-zA-Z]+(/([a-zA-Z]|[0-9])*)*$from> <to>/index.htmlto> rule> urlrewrite>
其中rule部分匹配地址用到了正则表达式,这里不赘述。这样就将规则设置完成了,重启tomcat刷新页面,没有#也不会404了。
Nginx用到的是try_files,修改nginx的配置文件,增加try_files配置。
server {
server_name localhost;
root /www;
location / {
try_files $uri $uri/ /index.html; } }
其中root的目录,对应ng-app的定义文件html的目录
ServerName localhost
DocumentRoot /www
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]
<system.webServer>
<rewrite>
<rules> <rule name="Main Rule" stopProcessing="true"> <match url=".*" >match> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" >add> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" >add> conditions> <action type="Rewrite" url="/" >action> rule> rules> rewrite> system.webServer>
var express = require('express');
var app = express();
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(8080); //the port you want to use