HTTPS: Laravel using .htaccess (public/) - laravel

I'm trying to include https in my Laravel Project without using godaddy only using htaccess and not that big deal since I'm using proxypass from my SSL certified website that will pass to my laravel project.
I just need to make my laravel project to https.
So I tried to add this code to my .htaccess located at public/
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://myproject.com/$1 [R=301,L]
Here's my public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
UPDATED
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
My httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/opt/lampp/htdocs/e-wallet-frontend/public"
ServerName localhost
<Directory "/opt/lampp/htdocs/e-wallet-frontend/public">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog "logs/frontendewallet.com-error_log"
CustomLog "logs/frontendewallet.com-access_log" common
</VirtualHost>
the HTTPS appears but the browser says Object not Found is there something wrong with my htaccess?

RewriteRule ^(.*)$ https://www.myproject.com/$1 [R=301,L]
This is the code I have on my multiple Laravel projects.

Related

Failed to load resource: the server responded with a status of 403 (Forbidden)- Laravel 8 problem with htaccess

I got error in my website after I add .htaccess file in the root folder of my website, I use laravel 8 and storage in my website, note i have run php artisan storage:link, please help me anyone. thank you
this is .htaccess configuration :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
and this is my .htaccess configuration in public directory :
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

alter laravel's htaccess such that it'll route to specific file

i want to route only /api/* (exact) to index.php
except that all the routes /* should endup in a static file directory which will have index.html
laravel's default.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
With your shown attempts, could you please try following Rules. Please make sure to clear your browser cache before testing your URLs.
This is considering that your index.php and htaccess files are present in /opt/lampp/htdocs/project/public path.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine ON
RewriteBase /project/public/
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
#Rules for /api/ folder here...
RewriteRule ^api/?$ index.php [L]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ dist/index.html [L]
</IfModule>

"Too many redirects" if forcing HTTPS, assets loaded via HTTP otherwise

I have installed an SSL certificate for my Laravel website, however when I open it as https://example.com the CSS/JS assets are loaded through HTTP. For example, asset('css/charts.css') amounts to http://example.com/public/css/charts.css and the browser refuses to load it because it's unsafe.
So I followed the answers to several SO questions to force all links to become HTTPS by doing the following:
1) added HTTPS_ONLY=true to .env
2) in app/Providers/AppServiceProvider.php I added the following:
if(env('HTTPS_ONLY')){
$url->forceScheme('https');
}
Inside the service provider's boot method. However, after I do that and try to open https://example.com I get a ERR_TOO_MANY_REDIRECTS error.
This is the vhost file for the domain:
<VirtualHost *:80>
ServerAdmin myemail#gmail.com
ServerName example.com
DocumentRoot /var/www/example.com/public
<Directory /var/www/example.com/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
So there is nothing related to HTTPS in there. In example.com/public there is an .htaccess file that I looked at too but its contents seem innocent as well:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Does anyone know what the issue might be and how I can debug this? This is all on Apache, Ubuntu 18.04 server.
asset,url helper will following this environment (.env) value, so change it like this
APP_URL=https://example.com
and run php artisan config:clear
I am using this config for https redirect
the following in appserviceprovider.php
if(config('app.env') === 'production') {
\URL::forceScheme('https');
}
.htaccess in public
Options -MultiViews -Indexes
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Recently, i did found something else, If there are assets that are loading on http from external links(like an image on http:://abcd.jpg), then your page will tend to load on http instead of https.
It will get stuck in a loop because the same asset will cause the page to open on http again, causing page to go back on http and redirection will go on.

Remove public folder in Laravel 5 using AWS EC2 or Beanstalk

In local environment I can navigatate to domain.com/app but in AWS it only works if I use domain.com/public/app. I have tried a number approaches to get it to resolve without public folder
I tried 2 different approaches already:
Added .htaccess mod_rewrite but it provides an error below.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Laravel NotFoundHttpException in compiled.php line 7610.
I do not want to rename the server.php to index.php as this is not an optimally secure approach.
In /public/.htaccess I have
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Any other way to get http to https without using Homestead?
In your virtualhost file point documentroot to public folder of laravel

Htaccess - Redirecting to core domain instead of same file

My domain requests are redirecting to the root of the domain instead of the given file type when attempting to redirect non www to www.
For example; I want website ---.com/dogs to go to www.---.com/dogs but instead it goes to www.---.com.
I'm using Laravel. What could be wrong?
My htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
#rewrite
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Virtual Host:
<VirtualHost 1.1.1.1:443>
SSLEngine on
SSLCertificateFile /root/sslcert/--.pem
SSLCertificateKeyFile /root/sslcert/--.key
ServerName www.--.com
#ServerAlias --.com
DocumentRoot "/var/www/website-live/public"
<Directory /var/www/website-live/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
I've tried removing the conditions i added for the redirect and no redirect occurs, so only with the two lines under #rewrite do these issues occur

Resources