【问题标题】:.htaccess 500 internal server error when routing api路由api时.htaccess 500内部服务器错误
【发布时间】:2020-09-17 11:14:44
【问题描述】:

我目前在本地的 apache 服务器上运行 php,带有 React 前端。

这就是我当前的.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 文件有问题吗?

【问题讨论】:

  • 请在.env文件中设置APP_DEBUG=true并检查错误。

标签: reactjs regex apache .htaccess mod-rewrite


【解决方案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]

当 URI 以 .php 结尾时,RewriteCond %{REQUEST_URI} !\.php$ [NC] 将跳过重写。

【讨论】:

  • 使用此解决方案不会加载任何内容。只是空白页。
  • 清空缓存并重新加载,检查网络选项卡没有 404 错误,但我仍然得到空白页。
  • 不,我没有,它们在 /static 文件夹中。
猜你喜欢
  • 2019-07-19
  • 2016-09-22
  • 2017-04-05
  • 1970-01-01
  • 2013-02-06
  • 2015-06-28
  • 2016-04-20
  • 2014-01-01
相关资源
最近更新 更多