即使在服务器端重写URL后,Angularjs HTML5模式仍然无效 [英] Angularjs HTML5 mode on doesnt work even after rewriting URLs on server side

查看:243
本文介绍了即使在服务器端重写URL后,Angularjs HTML5模式仍然无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my Angularjs application the '#' was present initially in the URL. So I set the HTML5 mode for the app to true and also made the necessary changes all over the application code for URLs. PFB my code for that.

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

现在根据此链接我在配置文件中进行了必要的更改。这些更改如下:

Now as per this link I made the necessary changes in my configuration file. Those changes are as follows:

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

问题:

应用程序仍然无法正常运行,但仍然无法刷新网址,即我仍然得到404:找不到页面错误。

The application still won't work and it still doesn't refresh the URL i.e. I still get the 404: Page not found error.

请帮我解决这个问题。

推荐答案

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

Well, setting requireBase: true and inserting <base href="/">(or what ever else, depending on the env) in the header, and using this settings

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

works for me in every places that i ever used it. Your DirectoryIndex must be set to index.html or the name of the file you are using and this is very important, your server must always send your initial file.

以下是我现在正在使用的一个例子,但它在我的电脑上,没有公共托管。

The following is an example, of what i'm using right now, but it is in my pc, no public hosting.

<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模式仍然无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆