Laravel 8 Force HTTPS Middleware failed I installed SSL in my production server . and i want to setup laravel application on it.
I followed up this article
https://robindirksen.nl/blog/laravel-redirect-to-https-a-middleware-to-force-https
But its failed. here is my codes
HTTPSProtocolMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class HttpsProtocolMiddleware
{
public function handle(Request $request, Closure $next)
{
if (!$request->secure() && app()->environment('production')) {
return redirect()->secure($request->getRequestUri());
}
return $next($request);
}
}
env
...
APP_ENV=production
...
Result url
https://boobamba.com/lms/public/lms/public/login
I can see duplicate url pharase "/lms/public/lms/public"
please help me to resolve this
Try putting this in your AppServiceProvider boot method
if($this->app->environment('production')) {
\URL::forceScheme('https');
}
it redirects to https:// version , but login url not works. :(
Please note : i created virtual hosts in centos7 server. does it affect that ?
<VirtualHost *:80>
ServerName samadhi.xxxxxxxxx.com
ServerAlias samadhi.xxxxxxxxx.com
ServerAdmin [email protected]
DocumentRoot /var/www/html/samadhi.xxxxxxxxx.com/public_html
<Directory /var/www/html/samadhi.xxxxxxxxx.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
#Disable index view
options -Indexes
#hide a Specifuc File
<Files .env>
order allow,deny
Deny from all
</Files>
</Directory>
</VirtualHost>
You should use "public" folder as your document root unless you renamed it to public_html ?
....
DocumentRoot /var/www/html/samadhi.xxxxxxxxx.com/public_html/public
<Directory /var/www/html/samadhi.xxxxxxxxx.com/public_html/public>
....
Also not sure if it might cause an issue but you have set options -Indexes twice.
Here is an example of configuration which works just fine for me:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName sub.domain.com
ServerAlias www.sub.domain.com
DocumentRoot /var/www/sub.domain.com/public
<Directory /var/www/sub.domain.com/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
You should not access your website through /public folder at all, that's what I'm trying to say. Please revert back any changes on your .htaccess file if you have changed it and use your "public" folder inside laravel project as document root. And update your APP_URL to https://xxxxxx.com
@vixo , i didn't change anything in .htaccess file . i m dealing with virtual host file only
Still its not working :(
<VirtualHost *:80>
ServerName samadhi.xxxx.com
ServerAlias samadhi.xxxx.com
ServerAdmin [email protected]
DocumentRoot /var/www/html/xxxx.com/public_html/public
<Directory /var/www/html/xxxx.com/public_html/public>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/www/html/xxxx.com/error.log
CustomLog /var/www/html/xxxx.com/requests.log combined
</VirtualHost>
Did you try restarting apache ?
Looks like we forgot about 443 port.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName mysite.example.com
Redirect permanent / https://mysite.example.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName mysite.example.com
#and the other config
</VirtualHost>
@vixo could you please provide full code.
@vixo , No worries. I modified my code . It works ! thanks for your guidance.
<VirtualHost *:443>
DocumentRoot "/var/www/html/samadhi.xxxxx.com/public_html/public"
ServerName samadhi.xxxxx.com:443
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/samadhi_xxxxx.com_com.crt
SSLCertificateKeyFile /etc/ssl/private/samadhi.xxxxx.com.com.key
<Directory /var/www/html/samadhi.xxxxx.com/public_html/public>
Options -Indexes +FollowSymLinks
AllowOverride All
#Disable index view
options -Indexes
#hide a Specifuc File
<Files .env>
order allow,deny
Deny from all
</Files>
</Directory>
ErrorLog /var/www/html/samadhixxxxx.com/error.log
CustomLog /var/www/html/samadhi.xxxxx.com/requests.log combined
</VirtualHost>
I optimized my code . Here is the final result
xxxxx.conf
<VirtualHost *:80>
ServerName http://samadhi.xxxx.com
Redirect / https://samadhi.xxxx.com/login
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "/var/www/html/samadhi.xxxxx.com/public_html/public"
ServerName samadhi.xxxxx.com:443
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/samadhi_xxxxx.com_com.crt
SSLCertificateKeyFile /etc/ssl/private/samadhi.xxxxx.com.com.key
<Directory /var/www/html/samadhi.xxxxx.com/public_html/public>
Options -Indexes +FollowSymLinks
AllowOverride All
#Disable index view
options -Indexes
#hide a Specifuc File
<Files .env>
order allow,deny
Deny from all
</Files>
</Directory>
ErrorLog /var/www/html/samadhixxxxx.com/error.log
CustomLog /var/www/html/samadhi.xxxxx.com/requests.log combined
</VirtualHost>
Please sign in or create an account to participate in this conversation.