Apache mod_rewrite Restful api

Geo*_*ber 2 apache .htaccess mod-rewrite

我在我的宁静 web 应用程序中遇到了正确的 .htaccess 问题。我的REST API代码应是通过URL模式访问* / API / **应重定向到的index.phpAPI文件夹中。

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

我有以下文件夹结构:

- api
-  应用/...
-  小贩/...
-- index.php
- 民众
-- css/...
-- js/...
- 索引.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]
Run Code Online (Sandbox Code Playgroud)

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

我认为较低的规则可能是正确的。因为重定向按预期工作。但是 /api/ 规则不起作用。似乎请求也重定向到 index.html

我错过了什么?

anu*_*ava 6

您需要对正则表达式模式进行细微调整:

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]
Run Code Online (Sandbox Code Playgroud)