我正在 angularjs 创建一个网站并使用 ngRoutes。我的查询是关于从 url 中删除 #
I am making a website in angularjs and using ngRoutes. My query is about removing # fromt the url
Following is my code where I am configuring my routes:
/**
* Main AngularJS Web Application
*/
var app = angular.module('chocomelte', [
'ngRoute'
]);
/**
* Configure the Routes
*/
app
.controller('HomeCtrl',HomeCtrl)
.controller('AboutCtrl',AboutCtrl)
.config(['$routeProvider','$locationProvider', function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when("/", {
templateUrl: "partials/home.html",
controller: "HomeCtrl"
})
.when("/about", {
templateUrl: "partials/about.html",
controller: "AboutCtrl"
})
}]);
这从我的 url 中删除了#。当我尝试通过单击 UI 中的按钮转到 /about 路线时,它工作正常。但是,当我手动尝试键入 URL 并转到它或在 /about url 上刷新页面时,我收到此错误:
500内部服务器错误。
我没有在后端使用节点。一个在前端使用 angular 的简单网站。我如何在客户端配置它?
确保使用此元标记设置基础 url:
<base href="/my-base">
此外,您可能必须在使用 html5 模式时在您的服务器上实现 URL 重写,以确保任何请求都被重定向到应用程序的根目录,而不是被解释为 GET请求。
Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application
我正在使用 Apache 服务器。终于修好了。不得不重写 .htaccess 文件中的 url,如下所示:
Options +FollowSymLinks
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
</ifModule>
必须指定 index.html 作为入口点。
Following is my code where I am configuring my routes:
/**
* Main AngularJS Web Application
*/
var app = angular.module('chocomelte', [
'ngRoute'
]);
/**
* Configure the Routes
*/
app
.controller('HomeCtrl',HomeCtrl)
.controller('AboutCtrl',AboutCtrl)
.config(['$routeProvider','$locationProvider', function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when("/", {
templateUrl: "partials/home.html",
controller: "HomeCtrl"
})
.when("/about", {
templateUrl: "partials/about.html",
controller: "AboutCtrl"
})
}]);
这从我的 url 中删除了#。当我尝试通过单击 UI 中的按钮转到 /about 路线时,它工作正常。但是,当我手动尝试键入 URL 并转到它或在 /about url 上刷新页面时,我收到此错误: 500内部服务器错误。 我没有在后端使用节点。一个在前端使用 angular 的简单网站。我如何在客户端配置它?
确保使用此元标记设置基础 url:
<base href="/my-base">
此外,您可能必须在使用 html5 模式时在您的服务器上实现 URL 重写,以确保任何请求都被重定向到应用程序的根目录,而不是被解释为 GET请求。
Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application
我正在使用 Apache 服务器。终于修好了。不得不重写 .htaccess 文件中的 url,如下所示:
Options +FollowSymLinks
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
</ifModule>
必须指定 index.html 作为入口点。