htaccess file is not working properly in Laravel on linux - php

I'm working on a Laravel project. I'm Removing "/public" from URL but it's only work on welcome page, other page like /login,/register is doesn't work properly. it's shows an error.
The requested URL /GME/login was not found on this server.
Apache/2.4.18 (Ubuntu) Server at localhost Port 80
but when I use public/index.php in URL it's working.
http://localhost/GME/public/index.php/login
Here, is my root .htaccess file code.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public/
RewriteRule ^(.*)$ public/$1 [L] #relative substitution
RewriteRule ^ index.php
and here is my /public/.htaccess file code.
<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]
note: i'm using linux elementary OS loki version

I think you need to add this to the GME directory .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

Related

How to configure .htaccess for a Laravel API app which is hosted on a subdomain?

I have a domain mydomain.com and a nginx space to host my website. The URL address mydomain.com is already in use and I don't want to buy another domain, so I created a subdomain myapp.mydomain.com. Then I created a laravel API app myapp for testing purposes and uploaded the code into /mydomain_com/myapp folder via a FTP client.
The problem now is I have to configure my .htaccess accordingly so the the app can run correctly.
At the current state, the homepage route / is working fine, but everything else is not working.
\mydomain_com\myapp.env:
...
APP_NAME=myapp
APP_ENV=local
APP_KEY=base64:U4v....
APP_DEBUG=false
APP_URL=https://myapp.mydomain.com
...
My routes are:
\mydomain_com\myapp\routes\web.php
Route::get('/', function () {
return view('welcome');
});
Route::get('/aaa', function () {
return view('welcome');
});
\mydomain_com\myapp\routes\api.php
Route::apiResource('os', OsController::class);
Now, when I go to https://myapp.mydomain.com I get this:
However when I try to visit https://myapp.mydomain.com/aaa and https://myapp.mydomain.com/api/os, I get this error ↓
Can You pls tell me what's the problem with my \mydomain_com\myapp\.htaccess ↓ ? Why is the homepage only working?
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} !^subdomain
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{HTTP_HOST} ^subdomain
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/ [L]
Im using the htaccess which is already with the laravel and Im posting it below, it works fine with me
<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>
Also check if the mod_rewrite is enabled in your server, if you are using ubuntu and apache2 server enable the mod_rewrite using the command
sudo a2enmod rewrite
Kindly refer : Enable mod_rewrite
Update the content of .htaccess file in the root folder with this. It should work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

How to change the root folder on the hosting

The site is written in Laravel, when you transfer the site to hosting, all access to ccs, js and photos is done at the link example.com/public/app.css instead of example.com/app.css
.htaccess file
DirectoryIndex public/index.php
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -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 ^ public/index.php [L]
</IfModule>
enter image description here
try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

Unable to get .htaccess to do 301 redirects correctly in a Laravel app

I got a URL Canonicalization issue and I want to redirect https://www.barreau.co/ar to https://barreau.co/ar
Please note that the following .htaccess code redirects https://www.barreau.co/ to https://barreau.co/ perfectly, but not with the /ar or /en that the Laravel multilanguage applications add.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.barreau\.co\/ar$
RewriteRule ^/?$ "http\:\/\/barreau\.co\/ar" [R=301,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]
How can I make both https://www.barreau.co/ and https://www.barreau.co/ar redirect to non-www URLs correctly?
There's need to specify the locale imo.
Can you try this?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I tried it in a local environment with a fresh Laravel installation. It works fine. Make sure you also have mod_rewrite enabled and to restart your apache after doing the change.

request exceeded the limit of 10 internal redirects laravel 5.5

I have a problem when uploading a new laravel 5.5 project. When I alter the htaccess in the public_html folder on my server it keeps redirecting. However this problem wasnt there on laravel 5.0. Here's my htaccess from the public_html folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
And here's the htaccess in the public folder:
<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 suggestions on how to solve this problem?
Beside the fact that it is really not good idea to put a Laravel project in your Document Root, you have a problem with your htaccess file in your Root directory.
Additional I would at least protect all files in your root folder from direct access:
<IfModule mod_rewrite.c>
RewriteEngine On
# Fake 404 for all files in /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?[^/]+$ - [R=404,L]
# Protect rewrite loop: exclude /public
RewriteCond %{REQUEST_URI} !^/?public(/.*)?$
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>

Laravel 5 htaccess on shared web. Works both with /public/ URL and without

I am using Laravel 5.4 on shared web hosting. Site is running fine, without any functional problems. I am forcing HTTPS + WWW always.
But there are issue with URL path after redirect from invalid/non-existing page.
Instead of https://www.example.com/ in URL it shows https://www.example.com/public/
There are no redirect to /public/ if I go to any existing pages. Site is working fine in both ways, with or without /public/. For visitors it could be confusing that sometimes it is /public/ in end of URL and sometimes it is without. For example, normally I have URL https://www.example.com/about , but it is also working in https://www.example.com/public/about In this case I am also not sure about possible vulnerabilities.
I have 2 .htaccess files, one in project root folder public_html and another inside Laravel public folder public_html/public.
1) public_html/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
2) public_html/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
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}]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(txt|xml)$ - [L]
# Force SSL + WWW
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
</IfModule>

Categories