【问题标题】:AngularJS and .htaccess redirectAngularJS 和 .htaccess 重定向
【发布时间】:2014-10-19 23:56:10
【问题描述】:

我正在使用 angularJs 构建网站。

我有以下文件夹结构:

/
--index.html
--js
  --contoller.js
--css
  --style.css
--pages
  --homepage.html
  --faq.html
  --about.html

索引页面上的相关html是:

<div id="app">
    <div ng-view>
        <!-- this is where the views are injected -->
    </div>
</div>
<li><a href="about">About</a></li>
<li><a href="about/faq">FAQ</a></li>

路由是:

obApp.config(function($routeProvider, $locationProvider) {
    $routeProvider
    // route for the home page
    .when('/', {
        templateUrl : 'pages/homepage.html',
        controller  : 'homepageController'
    })
    // route for the about page
    .when('/about/faq', {
        templateUrl : 'pages/faq.html',
        controller  : 'faqController'
    })
    // route for the contact page
    .when('/about', {
        templateUrl : 'pages/about.html',
        controller  : 'aboutController'
    })
})

.htaccess 文件是:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

现在这是我的问题: 当我访问链接时,一切正常 - 所以没关系。当我在浏览器地址栏中输入 url /about 时,它可以工作 - 再次正常。但是当我这样做时/about/faq 我让一切正常工作,但样式表没有加载。我检查了网络响应,但我得到了错误的内容类型:

您可以看到带有 google 字体的外部链接加载正常,只有本地 css 文件以错误的类型发送。

我的链接在标题中如下:

<link rel="stylesheet" type="text/css" href="css/style.css" >       
<!-- external font -->
<link href='http://fonts.googleapis.com/css?family=Courgette' rel='stylesheet' type='text/css'>

话虽如此,我被困住了。我在本地机器上开发这个,网址是这样的:

localhost/websites/angularProject

我的猜测是重定向需要额外的东西。 (??)

【问题讨论】:

    标签: regex angularjs .htaccess redirect


    【解决方案1】:

    改用绝对路径。

    您的链接

    <link rel="stylesheet" type="text/css" href="/websites/angularProject/css/style.css" >
    

    jsimages 链接执行相同操作。

    index.html

    <div id="app">
        <div ng-view>
            <!-- this is where the views are injected -->
        </div>
    </div>
    <li><a href="/websites/angularProject/about">About</a></li>
    <li><a href="/websites/angularProject/about/faq">FAQ</a></li>
    

    对于您的链接,您也可以使用&lt;base href="/websites/angularProject/" /&gt; html 标记而不是替换所有链接。

    htaccess

    RewriteEngine On
    RewriteBase /websites/angularProject/
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [L]
    

    【讨论】:

    • 我正在本地主机上开发这个,我有多个网站。我在 localhost 上的 url 类似于“localhost/websites/angularProject”。您的解决方案并不能帮助我摆脱我所处的束缚。但是我可能不够具体。编辑了我的帖子以反映这一点。
    • 这不是问题,我编辑了我的答案,让它像你想要的那样工作
    • 你有例子吗?
    • 这对我帮助很大。一直在寻找这个年龄。
    猜你喜欢
    • 2013-12-30
    • 2017-07-15
    • 2014-07-19
    • 2012-02-03
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    相关资源
    最近更新 更多