【问题标题】:Laravel route getting ruined when trying to reach HTTP request尝试到达 HTTP 请求时,Laravel 路由被破坏
【发布时间】:2019-12-20 06:24:47
【问题描述】:

目前我已经成功设置了我的Laravel Passport API

使用Laravel 5.8

我的 api 有这个 httpd-vhosts.conf 配置

端口:80

<VirtualHost *:80>
ServerName <sub-domain>.<domain>.com
ServerAlias <sub-domain>.<domain>.com
Redirect permanent / https://<sub-domain>.<domain>.com
</VirtualHost>

端口:443

 <VirtualHost *:443>
   DocumentRoot "/opt/lampp/htdocs/api_tk/public"
   <Directory "/opt/lampp/htdocs/api_tk/public">
      Options Indexes FollowSymLinks MultiViews
      AllowOverride All
      Order allow,deny
      allow from all
   </Directory>
   ServerName <sub-domain>.<domain>.com
   ServerAlias <sub-domain>.<domain>.com

   ErrorLog "logs/API-error_log"
   CustomLog "logs/API-access_log" common

   ProxyPreserveHost On
   ProxyRequests Off
   ProxyPassMatch /fingerprint http://localhost:5000
   ProxyPassReverse /fingerprint http://localhost:5000
   RequestHeader set X-Forwarded-Proto https
   RequestHeader set X-FOrwarded-Port 443

   SSLEngine on
   SSLCertificateFile "/opt/lampp/htdocs/ssl_key/svs-file.crt"
   SSLCertificateKeyFile "/opt/lampp/htdocs/ssl_key/private_new.key"
   SSLCACertificateFile "/opt/lampp/htdocs/ssl_key/svs-bundle-file.crt"
</VirtualHost>

443 工作正常,我可以在浏览器上看到我的 HTTPS SSL 锁定标志

但是

当我尝试做 HTTP 请求时

Laravel API 总是被毁掉。

我有这样的路线

https://..com/api/login

这很好用

但是当我尝试像这样进行 HTTP 请求时

http://&lt;sub-domain&gt;.&lt;domain&gt;.com/api/login

它总是以

https://&lt;sub-domain&gt;.&lt;domain&gt;.comapi/login

斜线缺失的地方。这是因为我的 PORT 80 配置上的 redirect permanent

我的 api 有这条路线。 (api.php)

Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});


Route::post('/timekeeping','Auth\Api\AuthController@timekeeping');

Route::post('/login','Auth\Api\AuthController@login');

Route::middleware('auth:api')->group(function () {

Route::post('/timekeeping_app','Auth\Api\AuthController@timekeeping_app');

Route::post('/logout','Auth\Api\AuthController@logout');

Route::post('/register','Auth\Api\AuthController@register');

Route::post('/show_dtr_list','Auth\Api\AuthController@show_dtr_list');

Route::post('/update','Auth\Api\AuthController@update');

Route::post('/delete','Auth\Api\AuthController@delete');

Route::post('/search_user','Auth\Api\AuthController@search_user');

Route::get('/current_time','Auth\Api\AuthController@current_time');

});

如何防止这种情况发生?

更新

试图像这样编辑我的虚拟主机配置

Redirect permanent "/" "https://&lt;sub-domain&gt;.&lt;domain&gt;.com/"

Redirect permanent / https://&lt;sub-domain&gt;.&lt;domain&gt;.com\/

但这给了我这样的输出

【问题讨论】:

  • 两种选择: 1. 尝试添加斜杠并引用网址Redirect permanent "/" "https://&lt;sub-domain&gt;.&lt;domain&gt;.com/";或 2. 尝试用反斜杠转义斜杠:Redirect permanent / https://&lt;sub-domain&gt;.&lt;domain&gt;.com\/
  • 这行得通,它重定向到 https 并到达我的 api 项目,但问题是如果我尝试执行这 2 个选项,这会给我这样的输出 https://&lt;sub-domain&gt;.&lt;domain&gt;.com//api/login 包含额外的斜杠,但它应该是https://&lt;sub-domain&gt;.&lt;domain&gt;.com/api/login

标签: laravel api xampp laravel-passport


【解决方案1】:

尝试在您的 apache 虚拟主机设置下添加斜杠:

<VirtualHost *:80>
ServerName <sub-domain>.<domain>.com
ServerAlias <sub-domain>.<domain>.com
# fix below
Redirect permanent / https://<sub-domain>.<domain>.com/
</VirtualHost>

【讨论】:

  • hmm... 不能保证 100%,但这不应该是 Laravel 的问题,这是 apache,所以请尝试一下,更改设置,尝试其他重定向,例如最后使用 /api ,请确保您重新启动 apache,无论如何...
猜你喜欢
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 2019-04-19
  • 2012-06-27
  • 2021-01-09
  • 2011-11-17
相关资源
最近更新 更多