2016-07-04 16 views
4

在我的Angularjs應用程序中,「#」最初出現在URL中。因此,我將該應用程序的HTML5 mode設置爲true,並對URL的應用程序代碼進行了必要的更改。 PFB我的代碼。即使在重寫服務器端的URL之後,Angularjs HTML5模式仍無法正常工作

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:找不到頁面錯誤。

請幫我解決這個問題。

+0

嘗試設置'requireBase:TRUE'和插入'<基本href = 「/」>'在您的index.html –

+0

安德魯的報頭,我這樣做。它仍然不起作用。這個問題似乎出現在我寫入配置文件的重寫規則中。我無法弄清楚問題所在。 –

+1

可以肯定的是,您的服務器必須始終發送您的初始index.html文件。路由以angularjs處理 –

回答

0

好,設置requireBase: true和插入<base href="/">(或別的什麼永遠,取決於ENV)的頭,並使用該設置

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> 
相關問題