Laravel redirect to HTTPS with .htaccess - laravel

I'm using Laravel 5.5.
Laravel has couple of .htaccess and in my case I'm working with .htaccess on root and inside the public folder.
All this time I was using this code in root .htaccess to setup the public folder:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
and my website is loading when I enter the homepage.
After that, I wanted to force redirect all the pages using https. So, first when I try to put this code inside the root .htaccess it doesn't do anything. It keeps loading with http.
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Well, it makes sense, because when I setup the public folder, I have to put the redirect inside the .htaccess that is located inside the public folder.
OK, so I've tried like this now:
The root .htaccess:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
The 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>
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This combination works. It does redirects to https.
But, there's one issue now. When I enter the homepage or if I open the website from the search results, the URL looks like this:
https://mywebsite.com/public/en
When I enter the other pages, I don't have issue related with the "public" path in the URL. So, let's say I want to open the contact page, the URL is https://mywebsite.com/en/contact. That's fine.
How can I avoid that "public" in my URL when I enter the homepage?

This should remove public from the url.
RewriteRule ^(.*)$ public/$1 [L]
I am not sure if this should go above
# Handle Front Controller...
or within it? Maybe try either one and see if either work?
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/$1 [L]
RewriteRule ^ index.php [L]

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
source

Related

Peroblem with laarvel project deployment to cpanel

Hope you guys are well, I have faced a problem deploying my laravel project on Cpanel. When I am hit my route URL or a root URL then it show me "404 not found"
This is the root url : https://laravel.tamimikbal.com/project1/
I have written this code on my root .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
And this the public/.htaccess file
<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>
The .htaccess is not redirecting properly to the public directory. I can access and navigate on your site using "https://laravel.tamimikbal.com/project1/public/".
Try this as the contents of the .htaccess file in your root:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I can better understand if you put how is the routing url should be looks like
anyway you can try this code:
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
while you don't have www. and https is activated on your domain you can use this code to make redirect from www. to none www and from http to https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
the rout request and file directory you can use this code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css|webp|JP2)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
if you want to redirect you system files to 404 page edit this code and replace your system folder and your 404 routing page
RewriteRule ^system/(.*) index.php?route=your 404 page [L]

change document root with .htaccess internal redirect not working properly

Hello my fellow developers,
i need to change my document root some specific folder which holds the source code of my laravel project on a shared hosting, since my hosting plan dont let me change default document root so i cannot change my default index so i want to have to redirect it using .htaccess
for far i have been able to internally redirect with the following code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ master/project/public/index.php
doing this i am able to redirect to desired folder, but for some reason it is not working for assets, i keep getting 404 on all the assets
master/project/public/css/[all styles]
master/project/public/js/[all scritps]
master/project/public/images/[all pictures]
master/project/public/fonts/[all fonts]
how can i achieve this without having to change urls,
internal redirect is working but i cannot figure out why all of my assets are returning 404
Try this
i m using shared hosting and this i m using for redirect asset to public dir
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
#all request to public dir
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
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>
To redirect all request to public
#all request to public dir
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
so i found solution to my problem,
this solution is what i ended up with.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !master/project/public/
RewriteRule (.*) /master/project/public/$1 [L]
this way you can change the default root of the your hosting using .htaccess file. hopefully it is helpful for everybody new to .htaccess

Rewrite rule not working, return 404 not found

i configure the htaccess as shown below
// this htaccess in /var/www/html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ blog1/public/ [L]
RewriteRule ^market(.*)$ blog2/laravel/ [L]
</IfModule>
this is the htacess in blog2
Options -MultiViews
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [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}]
I moved my index.php from laravel folder to blog2.
when i access http://website.com/market it will always go to the blog1 rule, not redirect to blog2/laravel.
there will be fine if http://website.com/blog2, but the reason i want to do this because want to make the url in this way http://website.com/market-us etc.
What's wrong with my configuration?
Try switching places for your two rewrite rules. Put ^market(.*)$ as the first one, and ^(.*)$ blog1/public/ [L] right under it.

redirect to secure connection thru .htaccess / exception admin location / framework Laravel

I want to redirect http to https and in addition I have to remove admin location because it does not work with https.
The site uses the Laravel framework.
At the code below I need to add the restriction:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?website\.com
RewriteRule ^(.*)$ https://www.website.com/$1 [R,L]
# 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>
This will do it for you:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
The second RewriteCond stops the rewrite from happening if it is the /admin/ URI. The redirection is done using R=301 which is a permanent redirection, for testing purposes I advise you change this to R=302 as this is temporary.
Make sure you clear your cache before testing this.

SEO friendly URL of opencart not working inside Codeigniter

I have a site which is mainly based on Code igniter.Inside that I have a store setup using opencart.Everything working fine until I try to user opencart seo friendly urls.
Once done that,it shows codeigniter 404 view.
How can I bypass CI routes when url is xyz.com/store
My Htaccess file is here :
# Customized error messages.
ErrorDocument 404 index.php
# Set the default handler.
DirectoryIndex index.php
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
The problem is likely caused in your .htaccess file. The default Codeigniter .htaccess file is set up to route all traffic to your public directories index.php. To prevent this you will need to set up a RewriteCond to exclude any directories you want to keep outside of codeigniter.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(store[^/]*)$
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

Resources