2013-03-13 141 views
6

我想用一個幾乎精確的結構來設置apache 2.2與AngularJS作爲一個從以下封閉的問題。Apache重寫規則不適用於angularjs

rewrite rules for apache 2 to use with angular js

我想重定向所有請求的應用程序/ index.html的,除了請求/ API。

我的目錄結構其次

  • /
  • /應用
  • /app/index.html
  • /應用/ CSS
  • /應用/ JS
  • /API

我正在嘗試在httpd-vhosts文件中應用這些重定向規則,將其作爲問題的正確答案https://stackoverflow.com/a/15284848/671095發佈。

<VirtualHost *:80> 
ServerName www.test.com 
DocumentRoot "C:/websites/test" 
Alias /CFIDE "C:/websites/CFIDE" 

RewriteEngine On 

# if a directory or a file exists, use it directly 
RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteCond %{REQUEST_URI} !/api 

# otherwise forward it to index.html 
RewriteRule ^.*$ - [NC,L] 
RewriteRule ^app/. /app/index.html [NC,L] 

</VirtualHost> 

但是,當我嘗試訪問/在瀏覽器中,我提出了一個空白屏幕。如果我在瀏覽器中輸入/app/index.html,但是我可以在瀏覽器中查看html頁面,但是沒有一個css.or js可以訪問並且返回404。堅持這個看起來像一個簡單的事情來完成一個Apache重寫。

任何幫助,將不勝感激

回答

2

我有同樣的問題。但我在mod_rewrite無知,所以不得不穀歌很多。 我發現這個電子郵件解決方案:

https://groups.google.com/d/msg/angular/GmNiNVyvonk/mmffPbIcnRoJ

因此,我認爲你的.htaccess應該如下所示:

Options +FollowSymLinks 
IndexIgnore */* 

RewriteEngine on 

# if a directory or a file exists, use it directly 
RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteCond %{REQUEST_URI} !/api 
RewriteRule ^.*$ - [NC,L] 

# otherwise forward it to index.html 
RewriteRule ^app/(.*) /app/#/$1 [NC,L] 

通知的(.*)#/$1

注意:必須在你的包含CSS,JS等中使用絕對路徑,如果不是,你將會得到錯誤:

resource interpreted as script but transferred with mime type text/html

+0

我已經將這些規則應用於httpd-vhosts文件,但是當我嘗試訪問mywebsite.com時,它現在還沒有傳遞給index.html文件,現在當我訪問mywebsite.com/app /'我時在瀏覽器中顯示爲404 – matthew 2013-03-14 23:52:48

+0

我使用了您的代碼,並看到「內部服務器錯誤」消息。有想法該怎麼解決這個嗎? – Devner 2015-10-23 11:02:07

13

使用FallbackResource指令,現在在Apache 2.2.16+中這更容易了。

FallbackResource /app/index.html 

http://httpd.apache.org/docs/2.2/mod/mod_dir.html#fallbackresource

取決於你如何轉發訪問您的API,你可能還需要利用機箱取消對具體的API請求(2.2.24+)的後備資源。

<Directory /api> 
    FallbackResource disabled 
</Directory> 
+0

像apache 2.4.x的魅力一樣工作! – 2016-12-28 19:30:55

11

This作品對我來說:

<ifModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !index 
    RewriteRule (.*) index.html [L] 
</ifModule> 
+0

只需添加此評論,如果您的Angular應用程序不在主機的根目錄,即內容位於http://example.com/moo/等目錄中,則需要添加適當的「」標籤。 對於http://example.com/moo/的示例,您的代碼需要爲 - 這樣您的資產和模板才能正確加載 – BahamutWC 2013-07-10 17:26:59

+0

@BhamhamWC:使用基地標籤不鼓勵; http://docs.angularjs.org/guide/dev_guide.services.$location – Daan 2013-12-17 13:24:14

+1

@ Daan只要Angular建議您從根文檔提供它,它就只會被阻止。然而,如果你不能出於任何原因,你必須使用''標籤。 – BahamutWC 2013-12-18 18:25:47

1

不知道如果你仍然有這個問題,但我是有AngularJS相同的症狀和Apache 2

我的問題實際上,我使用的是IE,它自動地以兼容模式出現。我在HTML中使用了下面的行,它工作。

<meta http-equiv="X-UA-Compatible" content="IE=edge" /> 

在此進一步描述:< What does <meta http-equiv="X-UA-Compatible" content="IE=edge"> do?>。