即使在服务器端重写 URL 后,Angularjs HTML5 模式也不起作用

标签 angularjs html url-rewriting

在我的 Angularjs 应用程序中,“#”最初出现在 URL 中。因此,我将应用程序的 HTML5 模式 设置为 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:未找到页面错误。

请帮我解决这个问题。

最佳答案

嗯,设置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或您正在使用的文件的名称,这非常重要,您的服务器必须始终发送您的初始文件。

以下是我现在正在使用的示例,但它在我的电脑中,没有公共(public)主机。

<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>

关于即使在服务器端重写 URL 后,Angularjs HTML5 模式也不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38186723/

相关文章:

asp.net-mvc - 使用 Razor 引擎在mvc4中重写url

iis - 静态网页的url重写规则-使用带附加参数的url后返回404

apache - 将 RewriteCond 与 %{REQUEST_URI} 一起用于 mod_rewrite

Angularjs 动态 ng 模式验证

angularjs - Angular,没有浏览器?

javascript - 自定义产品照片

html - 无法更改 html 'option' 标签内 'select' 的背景颜色

angularjs - Protractor 使用 element.all 检查元素是否存在于 LIST 上

javascript - 使用 Angular 加载的新 View 未在 View 顶部加载

javascript - 为什么这个 html5 音频在 chrome 上不起作用,但在 safari 上运行良好