【发布时间】:2016-10-20 11:41:14
【问题描述】:
总结:
使用<base href="/prod/public/" />时,会在路由url中添加目录路径e.x.http://www.example.com/prod/public/home
我的 Angular 应用托管在 prod/public 目录中。
在我的public/index.html:
<base href="/prod/public/" />
我的路线是这样的:
$stateProvider
.state('home', {
url: '/home',
templateUrl: function($stateParams) {
var path = 'app/users/views/home.html';
return path;
},
controller: 'HomeCtrl'
})
目录结构:
public_html
-.htaccess
- other folders
- prod
- public
- app
- index.html
- bower.json
- .htaccess
- package.json
- gulpfile.js
- Readme.md
.htaccess 在 prod 目录中:
RewriteEngine On
Options -Indexes
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ public/index.html [L]
这是我在模板中引用状态的方式:
<a ui-sref='home'>Go to home</a>
当点击此链接时,它会重定向到此网址:http://www.example.com/prod/public/home
要求:
网址应该是 http://www.example.com/home 而不是 http://www.example.com/prod/public/home
【问题讨论】:
-
这就是
base href应该做的。如果您不希望它这样做,请不要使用它...? -
为什么要加碱基?
-
你在使用 Angularjs 的 Html5Mode 吗?通常的 Angular 路由行为缺少哈希。
-
@Boris 是的,我使用的是 Html5Mode。
-
@Shreejibawa 需要添加基础以提供应用程序资产的相对路径,因为我从 url 中删除了哈希标签
标签: angularjs .htaccess angular-ui-router ui-sref base-tag