从 angularjs + ui-router 中的 .htaccess 获取参数
Get Param from .htaccess in angularjs + ui-router
我的应用是多语言(angular-translate
)angularjs
应用 ui-router
。我在从 .htaccess
重定向获取参数时遇到问题。
我要的是下面的路由
www.example.com/en
= 英文版。
www.example.com/he
= 希伯来语版本。
www.example.com/ar
= 阿拉伯文版本。
- 任何其他重定向到英文版本。
在客户端
我的 app.config
看起来像这样:
Language State
$stateProvider.state('app', {
abstract: true,
url: '/:lang',
template: '<sh-app/>',
controller: function ($translate, $state, $rootScope) {
$translate.use($state.params.lang);
if (/he|ar/.test($state.params.lang)) {
$rootScope.dir = 'rtl';
}
}
});
Home State
$stateProvider.state('app.home', {
url: '/',
template: '<sh-main/>'
});
And of course to remove the hashbang
$locationProvider.html5Mode(true);
现在到服务器端。
我添加了一个 .htaccess
文件,其中包含以下内容:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /7for70/index.html [L]
</IfModule>
问题是,如果我尝试导航到 www.example.com/ar
,它只会重定向到 www.example.com/en/
和 $stateParams.lang === 'en'
。
如何将 url 的结尾转发给 ui-路由器。
对于遇到同样问题的任何人,经过两天的撕心裂肺,我有了一个主意。
我在我的 url 中添加了一个尾部斜杠,如下所示:
www.example.com/en => www.example.com/en/
www.example.com/he => www.example.com/he/
www.example.com/ar => www.example.com/ar/
和中提琴,成功了!
从那里很容易在 ui-router docs 中找到解决尾部斜杠的方法。
For older version of ui-router, add this snippet to your module's config function. It creates a rule on $urlRouterProvider that maps all urls that are missing a trailing slash to the same url but with the trailing slash appended.
$urlRouterProvider.rule(function ($injector, $location) {
var path = $location.url();
// check to see if the path already has a slash where it should be
if (path[path.length - 1] === '/' || path.indexOf('/?') > -1) {
return;
}
if (path.indexOf('?') > -1) {
return path.replace('?', '/?');
}
return path + '/';
});
编辑:(约 20 分钟后)
您似乎需要在抽象状态中添加尾部斜杠,然后将其从其余部分中删除。
所以抽象状态是:url: '/:lang/'
然后主状态将是 url: ''
我的应用是多语言(angular-translate
)angularjs
应用 ui-router
。我在从 .htaccess
重定向获取参数时遇到问题。
我要的是下面的路由
www.example.com/en
= 英文版。www.example.com/he
= 希伯来语版本。www.example.com/ar
= 阿拉伯文版本。- 任何其他重定向到英文版本。
在客户端
我的 app.config
看起来像这样:
Language State
$stateProvider.state('app', {
abstract: true,
url: '/:lang',
template: '<sh-app/>',
controller: function ($translate, $state, $rootScope) {
$translate.use($state.params.lang);
if (/he|ar/.test($state.params.lang)) {
$rootScope.dir = 'rtl';
}
}
});
Home State
$stateProvider.state('app.home', {
url: '/',
template: '<sh-main/>'
});
And of course to remove the hashbang
$locationProvider.html5Mode(true);
现在到服务器端。
我添加了一个 .htaccess
文件,其中包含以下内容:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /7for70/index.html [L]
</IfModule>
问题是,如果我尝试导航到 www.example.com/ar
,它只会重定向到 www.example.com/en/
和 $stateParams.lang === 'en'
。
如何将 url 的结尾转发给 ui-路由器。
对于遇到同样问题的任何人,经过两天的撕心裂肺,我有了一个主意。
我在我的 url 中添加了一个尾部斜杠,如下所示:
www.example.com/en => www.example.com/en/
www.example.com/he => www.example.com/he/
www.example.com/ar => www.example.com/ar/
和中提琴,成功了!
从那里很容易在 ui-router docs 中找到解决尾部斜杠的方法。
For older version of ui-router, add this snippet to your module's config function. It creates a rule on $urlRouterProvider that maps all urls that are missing a trailing slash to the same url but with the trailing slash appended.
$urlRouterProvider.rule(function ($injector, $location) {
var path = $location.url();
// check to see if the path already has a slash where it should be
if (path[path.length - 1] === '/' || path.indexOf('/?') > -1) {
return;
}
if (path.indexOf('?') > -1) {
return path.replace('?', '/?');
}
return path + '/';
});
编辑:(约 20 分钟后)
您似乎需要在抽象状态中添加尾部斜杠,然后将其从其余部分中删除。
所以抽象状态是:url: '/:lang/'
然后主状态将是 url: ''