2015-04-25 171 views
1

我在正确的.htaccess问题上停留在我宁静的webapp中。我的REST api代码应该可以通过URL模式*/api/**访问,它应该被重定向到index.phpapi文件夹中。Apache mod_rewrite Restful api

所有其他URL(静态文件除外)应重定向到根文件夹中的index.html

我有以下文件夹结构:

 
- api 
-- application/... 
-- vendor/... 
-- index.php 
- public 
-- css/... 
-- js/... 
- index.html 
- .htaccess 

,这是我的.htaccess内容:

Options +FollowSymLinks 
RewriteEngine on 

# redirect all api calls to /api/index.php 
RewriteCond "%{REQUEST_URI}" "^/api" 
RewriteRule "^/api/(.+)$" "/api/index.php$" [QSA,L] 

# If the request is a file, folder or symlink that exists, serve it up 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^(.+)$ - [S=1] 

# otherwise, serve your index.html app 
RewriteRule ^(.+)$ /index.html [L] 

我尝试另一个的.htaccess添加到API文件夹重定向与网址/ api/*但没有任何成功。所以我试图用上面的.htaccess中的规则重定向它。

我认为规则可能是正确的。因为重定向工作正常。但是/ api /规则不起作用。看来这些请求也被重定向到index.html

我错过了什么?

+0

删除'$'从得到的路径 – Deadooshka

回答

2

你的正则表达式模式的一些小改动就是你需要:

DirectoryIndex index.php index.html 
Options +FollowSymLinks 
RewriteEngine on 

# redirect all api calls to /api/index.php 
RewriteRule ^api/((?!index\.php$).+)$ api/index.php [L,NC] 

# If the request is a file, folder or symlink that exists, serve it up 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule^- [L] 

# otherwise, serve your index.html app 
RewriteRule ^(.+)$ index.html [L] 
+1

谢谢。它的作品像一个魅力:) –

+1

对不起,以为我接受它,但只是标记为有用;) –

+0

没有问题,很高兴它为你工作。 – anubhava

1

试试下面的设置规则:

Options +FollowSymLinks 
RewriteEngine On 

RewriteCond %{REQUEST_URI} !^/api/index\.php$ [NC] 
RewriteCond %{REQUEST_URI} ^/api [NC] 
RewriteRule ^api/.* /api/index.php$ [QSA,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-l # to check for symbolic links 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule .* /index.html [L] 

RewriteRule . - [L]