Why am I getting this laravel url routing error in .htaccess? - php

I have recently installed Laravel into my public_html folder. I want it so when I log into www.malhub.com it gets the contents of the public folder (public_html/public).
After trial and error, I was able to get it working somewhat. Now I have a 404 error, which is caused because the site (www.malhub.com) resolves to :
http://malhub.com/public/public_html
But my .htaccess code states:
RewriteRule ^(.*)$ public_html/public/$1 [L]
In other words, it looks for a public_html folder within the public folder (should just be the public folder)
I added some folders (test) just to make sense of how this works and am befuddled. It is going into the public folder and looking for another folder.
When I just try writing:
RewriteRule ^(.*)$ public/$1 [L]
or "public_html" or any combination of /../../ I keep getting 500 errors (that don't show the url).
How does this line of code work, and what is the optimum way?

By default the website will be load from public folder.
If you want to remove public from your url,copy .htaccess file from public folder to root and replace the code with the following..
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

If you don't want to remove all files and folder from the public folder, then just copy .htaccess to your root directory and rename server.php to index.php and last one step is if all resource files in /public directory couldn't find and request URLs didn't work for using asset() helper. Then, you need to add public to your helpers.php file.
function asset($path, $secure = null)
{
return app('url')->asset($path, $secure);
}
To
function asset($path, $secure = null)
{
return app('url')->asset("public/".$path, $secure);
}
You will fine the helpers.php in vendor/laravel/framework/src/Illuminate/Foundation/helpers.php.

Related

Laravel - unable to find new page (404 error - not found)

I´m looking for solution for my laravel project problem (laravel version: 6.5.2). I found a lot of similar questions, but no solution.
So, I have created new laravel project and then i put it on my hosting.
My FTP structure is:
- www
- AutsokolaIS
In www folder, I copied content of Public folder and in the AutoskolaIS other folders and files (app, bootstrap, config, etc.)
Everything was good, until I tried to create a new page linked to welcome page.
Link is mydomain.com/page. After click on this link, I get 404 error page. When I directly open the URL mydomain.com/index.php/page everything works.
I know it´s problem with .htaccess file, but I´m not able to edit .htaccess file in correct form.
Can anyone please help me?
Thank you
By default the website will be load from public folder. If you want to remove public from your url,copy .htaccess file from public folder to root and replace the code with the following..
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

Apache Document Root using .htaccess

I have a directory structure
mymvc
|--App
|--Core
|--logs
|--public
|--index.php
|--vendor
|--.htaccess
what i want is that if someone hit my url www.example.com/mymvc/ then all the request must go through public->index.php using .htaccess file. i do not have access to httpd.conf file.
|--public
|--index.php
I want my public folder to be accessible only as a document root and request pass through index.php file inside public folder. No one can access directly App , Core , logs etc. directories. Means i want my public folder to be DOCUMENT ROOT.
First you need to create an .htaccess file in the root of the project.
mymvc/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) public/$1 [L]
Then, in the .htaccess file in the public directory (which would be mymvc/public/.htaccess), you need to add a RewriteBase directive to the existing code, so it looks like this:
mymvc/public/.htaccess
# Remove the question mark from the request but maintain the query string
RewriteEngine On
RewriteBase /mymvc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

htaccess for Silex from the root folder not working as intended

I tried to search and use the solutions already posted but nothing worked for now and I tried pretty much everything I could but didn't work.
These are the app directories:
localhost/
root/
data/
app/
public/
index.php
.htaccess
The htaccess is this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /root/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
The first problem is that if I go to localhost/root/public the website works as intended but in the url I still see 'public' which I'd like to remove.
The second problem is that my objective is to go to the index from the url localhost/root/ but now, if I go there it just shows the files in the folder.
Thank you.
As requested, these are the routes that I use:
$app->get('/', function () use ($app) {
// code
})->bind('home');
$app->get('/about-me', function () use ($app) {
// code
})->bind('about-me');
There are two mistakes in your system.
First:
The .htaccess located in the root folder should be used to remove the 'public' part of the URL. The code for that is like the following:
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
It basically grabs every route and redirects it to the public folder, exactly as you wished!
Second:
You should create another .htaccess file in your '/public' folder to redirect everything to the index.php file. The code for that should look like this:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
You should end up with the following structure:
localhost/
root/
.htaccess (removes 'public')
data/
app/
public/
index.php
.htaccess (forward to index.php)
This should solve both your problems. If it does, please mark the question as completed by marking my answer as the one that solved it. If it does not, please comment on other problems that occur to you!

Precautions after Removing 'public' from URL in Laravel 5

I tried to find a solution to remove the 'public' from the url in a laravel 5 application. (localhost/laravelapp/public)
This SO answer was very useful but since i have used the asset helper, css, js and custom folder for uploading documents couldn't be found. So i had to try different answers to find the one that worked for me. While some answers suggest it's not good to remove the public folder at all, other suggest it's better to rename it instead of removing it.
I'm just wondering if there are any dangers or if there is anything i missed while removing the 'public' folder as per the answer i found in SO.
Here is what i did
Just like KA_lin's suggestion
Renamed the server.php to index.php (no modifications)
Copied the .htaccess from public to root
Changed .htaccess to the following
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
</IfModule>
Now, the css and js files were not loading properly and as per Sean Perkins answer, i opened vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php and changed
public function asset($path, $secure = null)
{
if ($this->isValidUrl($path)) {
return $path;
}
$root = $this->getRootUrl($this->getScheme($secure));
return $this->removeIndex($root).'/'.trim($path, '/');
}
to
public function asset($path, $secure = null)
{
if ($this->isValidUrl($path)) {
return $path;
}
$root = $this->getRootUrl($this->getScheme($secure));
return $this->removeIndex($root).'/public/'.trim($path, '/');
}
Now everything seems to work fine.
After that, i also added the following line in .htaccess since the .env file was being viewed when i requested localhost/laravelapp/.env
RewriteRule ^.env - [F,L,NC]
Now everything seems ready but i was wondering if there are any other precautions i should take before i upload the website to a live server.
i also noticed that the website will load all the content without css or js when i navigate to localhost/laravelapp/public.
Should i be worried about it.
I also didn't delete the .htaccess inside the public folder. I'm assuming its not a problem since i have a new one inside the root folder. Is it ok to delete it.

Is it good practice to make system root unable to be accessed?

Let's say we have the following schema:
root/
application/
-public/
index.php
css/
img/
javascript/
...
system/
...
I was thinking that if I create an .htaccess file in the root with the following rules:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ application/public/index.php?url=$1 [QSA,L]
I would be able to make other folder except public unable to be accessed. Assume index.php is the file that load all the file the application need and include and require them (with relative paths obliviously): the application is actually run in index.php and every public file such as css stylesheet, images or javascript script are in the public folder. So you, as user, cannot access the system folder because if you digit www.site.com/ it refers to root/application/public/. Am I right?
But the htaccess rule I set there does not work... If I put that htaccess in the root folder and I try to access the root folder the browser shows me the list of the file of the root folder (application and system). Why isn't it working?
I used this while ago when index.php was in the root folder:
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
and it worked just fine. Why isn't the new one working?
The document root of you project should be set to the /public folder.
This way that's the only folder to web-user would have access to.
Edit: for more info check the apache manual http://httpd.apache.org/docs/2.0/mod/core.html#documentroot
My initial code was right. Except for the third line which obliviously considered / a folder and made me see it instead of redirecting to application/public/.
Commenting the third line everything worked fine.
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ application/public/index.php?url=$1 [QSA,L]

Categories