2016-04-25 19 views
0

這是我的文件夾結構:AngularJS + PHP API

myproject/ 
    api/ 
     api.php 
    public/ 
     .htaccess 
     index.html 
     scripts/ 
      ... all my angular stuff... 

我的.htaccess就像如下:

RewriteEngine On 
# If an existing asset or directory is requested go to it as it is 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR] 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d 
RewriteRule^- [L] 

RewriteRule ^/api/ ../api/api.php 

# If the requested resource doesn't exist, use index.html 
RewriteRule^/index.html 

所以,我想這樣的:訪問http://localhost剛剛返回我的index.html。但是,我的AngularJs代碼中的Ajax調用/api/something應該由我的api.php文件處理。

目前,我的Apache虛擬主機指向myproject/public。訪問http://localhost工作正常(它返回index.html),但我的電話/api/something也返回index.html。看來api.php永遠不會到達。有任何想法嗎?

回答

2

我不認爲你可以使它與雙點工作,但你沒有結束規則的處理。

因此,它匹配的API後,它會在你的最後一條規則,並重寫以index.html的

在您的API行的末尾添加[L],就像這樣:

RewriteRule ^/api/ ../api/api.php [L] 

添加這一點,因爲它可能會需要:),如果你想知道什麼「東西」是在你的api.php你可以通過休息或URL(API後)作爲參數

RewriteRule ^api/(.+)$ <whatever_url_work>/api.php?action=$1 [NC,L] 
+1

作爲我懷疑,當你打apa時,相對論不會奏效che,看到這個可能的解決方案../api:http://stackoverflow.com/a/21324642/3802077 – user3802077

+0

酷。我會在到家時嘗試。謝謝! –