【问题标题】:Angularjs HTML5 mode on doesnt work even after rewriting URLs on server side即使在服务器端重写 URL 后,Angularjs HTML5 模式也不起作用
【发布时间】:2016-11-06 07:18:15
【问题描述】:

在我的Angularjs 应用程序中,“#”最初出现在 URL 中。因此,我将应用程序的 HTML5 mode 设置为 true,并在整个应用程序代码中为 URL 进行了必要的更改。 PFB 我的代码。

app.config(function($locationProvider){ $locationProvider.html5Mode({
    enabled: true,
    requireBase: false
});
});

现在根据this link,我在配置文件中进行了必要的更改。这些变化如下:

Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1 [L]

问题:

应用程序仍然无法运行,它仍然没有刷新 URL,即我仍然收到 404: Page not found 错误。

请帮我解决这个问题。

【问题讨论】:

  • 尝试设置 requireBase: true 并在 index.html 的标题中插入 <base href="/">
  • 安德鲁,我做到了。它仍然不起作用。问题似乎出在我在配置文件中编写的重写规则中。我无法弄清楚问题所在。
  • 可以肯定的是,您的服务器必须始终发送您的初始 index.html 文件。路由在 angularjs 中处理

标签: angularjs html url-rewriting


【解决方案1】:

好吧,设置requireBase: true 并在标题中插入<base href="/">(或其他任何内容,取决于环境),并使用此设置

    RewriteEngine On

    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]

在我曾经使用过的每个地方都为我工作。您的DirectoryIndex 必须设置为index.html 或您正在使用的文件的名称,这非常重要,您的服务器必须始终发送您的初始文件。

以下是我现在正在使用的示例,但它在我的电脑中,没有公共托管。

<VirtualHost 127.0.0.1:80>
    ServerName www.myproject.localhost
    DocumentRoot "d:/Work/myproject/"
    DirectoryIndex index.html
    <Directory "d:/Work/myproject/">
        Options +FollowSymlinks
        RewriteEngine On

        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]

        AllowOverride All
        Allow from All
        Require all granted
    </Directory>
</VirtualHost>

【讨论】:

    猜你喜欢
    • 2018-05-31
    • 1970-01-01
    • 2015-04-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多