我正在尝试确保Laravel项目中使用的所有路由都使用https.
我尝试通过在filter.php文件的App :: before()过滤器中添加重定向来做到这一点,如下所示:
if (!Request::secure()){
return Redirect::secure(Request::path());
}
这肯定会将我的网址改写为使用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站点的virtualhost中缺少配置项.
<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上,常规网址就可以与https一起使用,例如http://beta.mysite.com/register和https://beta.mysite.com/register将路由到同一位置.
这样,默认的.htaccess文件和安全路由重定向即可工作.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。