我正在努力确保我的 Laravel 项目中使用的所有路由都使用 https
。
我尝试通过在 `filter.php 文件的 App::before()
过滤器中添加一个重定向来做到这一点,如下所示:
if (!Request::secure()){
return Redirect::secure(Request::path());
}
这确实重写了我的 url 以使用 https
而不是 http
,但是页面返回时出现 Apache 错误:
Not Found
The requested URL /register was not found on this server.
Apache/2.4.9 (Ubuntu) Server at beta.mysite.com Port 443
我还需要更改其他内容以允许 Laravel 看到 https 请求吗?
编辑:这是我当前的 .htaccess
文件的内容:
<IfModule mod_rewrite.c>
#Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://beta.mysite.com/$1 [R,L]
</IfModule>
最佳答案
我遇到的问题实际上是 https 站点的虚拟主机中缺少配置项。
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/public
# The section I was missing START
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
# The section I was missing End
...
这些规则与在 http 虚拟主机(端口 80)中找到的规则相同。如果没有这些,laravel 就无法获取正确的路线。
一旦我为端口 443 设置好这些,常规 url 就可以使用 https,例如http://beta.mysite.com/register
和 https://beta.mysite.com/register
会路由到同一个地方。
有了这个,默认的 .htaccess 文件和安全路由重定向就可以工作了。
关于php - 在 Laravel 中使用 https 会抛出 apache 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24334397/