我目前正在本地的apache服务器上运行带有React前端的php。
这是我当前的.htaccess
的布局方式:
Options -MultiViews
RewriteEngine On
RewriteRule ^api/(.*)$ api/$1\.php [L]
RewriteCond %{REQUEST_URI} !^/api.*?
RewriteRule ^ index.html [QSA,L]
最基本的条件是路由可以在我的React应用程序中工作。然后,我要进行生产构建并将其复制到我的htdocs中。
该路由有效,但是,我希望能够在我的/api
目录中调用.php文件,而无需使用文件扩展名。因此,我希望将/api/
之后的所有内容重定向到输入的内容,然后再重定向到.php
。
例如/api/authentication
将转到/api/authentication.php
,/api/register
将转到/api/register.php
,依此类推。
在这种当前设置下,向/api/authentication
等发出请求时出现500个内部服务器错误。
我的.htaccess
文件有问题吗?
答案 0 :(得分:1)
当您匹配.*
时,您的第一个规则正在循环。您可以使用:
Options -MultiViews
RewriteEngine On
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteRule ^api/(.+)$ api/$1.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^api index.html [L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
将在URI以.php
结尾时跳过重写。