Laravel home page gives 404 but index.php works - php

I'm creating a custom cms in a subfolder of my site. I installed a fresh copy of laravel at www.mysite.com/cms/ <-- application, bundles, public, etc are there.
I can not make a virtual host due to shared hosting but I managed to remove the need for the /public to be in the url by putting
RewriteCond %{REQUEST_URI} !^/cms/public/.*$
RewriteRule ^(.*)$ /cms/public/$1 [L]
in the .htaccess file in my cms folder.
The thing is, clean urls works www.mysite.com/cms/public/ shows the Laravel starter page. But www.mysite.com/cms/ gives me a 404 not found error and www.mysite.com/cms/index.php shows the Laravel starter page.
I have set the url array element in application.php to
'url' => 'http://www.mysite.com/cms/',
and the index element to
'index' => '',
Has anyone come across this issue?

To use laravel on shared hosting, I would recommend to try this.
By the way, in your .htaccess, you are just redirecting to all non-exist request query to index.php in public directory. Because of the fact that, you should access with url.com/public.

Perhaps if you were to upload the contents of your public folder into /cms, and the other folders into /cms_system, you could change the paths.php file to reflect this?
So, you'd have this:
/cms
(contents of the public folder, with .htaccess)
/cms_system
/application
/bundles
/laravel
/storage
(etc)
Your paths would then look like this:
$paths['sys'] = 'system';
$paths['bundle'] = 'bundles';
$paths['app'] = 'application';
$paths['storage'] = 'storage';
$paths['public'] = '../cms';
I haven't tested this yet, but it should work. Please let me know if there are any problems with it, and will see what can be changed accordingly.

Related

How to deploy a Laravel app to a sub-directory

I'm new to Laravel framework so this question might be a little stupid but here's the problem:
I have a small project on my local machine and now I need to deploy my project to a live environment ( shared hosting ).
The problem is, my project needs to be stored in a sub-directory ( sub-folder ) of the main domain ( eg. my domain will be main.com, and the project will be at main.com/my-project ).
I've created a sub folder inside the public_html of the main domain, then I uploaded my project to that sub-directory. At first, when I went to the url main.com/my-project, I see a directory tree ( like localhost ), then I needed to click to public folder to go to my app. So I looked for the solutions on the internet and I tried the .htaccess way, here's my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/project/public/
RewriteRule ^(.*)$ /project/public/$1 [L,QSA]
</IfModule>
But when I reload the url main.com/my-project, I kept receiving the message URL request not found.
I've been struggling with this issue for a while and still haven't figured it out. Any idea how I can solve this problem? Please help me out, thanks.
Thank you guys for helping, well actually I figured it out somehow. Here's my solutions for anybody who might run into the same problem like I did:
Inside /public_html, I created a folder and named it my-project. Inside my-project folder, I placed all files & assets folders ( css, js ) and .htaccess file here.
Then I went back to the parent level ( same level as /public_html ), I created a folder and name it app-core. I placed all the other Laravel's folders & files here.
The structure is gonna be like this :
public_html
=app-core
--- All others files & folders except **public**
=my-project
--- index.php
--- css
--- js
--- .htaccess
Finally, I went back to edit the index.php like this :
require __DIR__ . '/../app-core/vendor/autoload.php';
$app = require_once __DIR__ . '/../app-core/bootstrap/app.php';
And saved it, then everything was ready to go!

Laravel redirect issue for custom root folder

My ISP doesn't allow my cpanel hosted laravel site to have a custom root folder (the public folder within Laravel's structure).
This means I can't make the server see /public_html/public as the root. It has to always be /public_html
Following their guidance I have setup the following /public_html/.htaccess rule to make the Laravel public folder appear as the root folder
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]
This works more or less but NOT completely. The issue is that if I request a page on my site with an ending backslash, the page url ends up as
www.domain-name.com/public/contact-us
instead of what I would expect which is
www.domain-name.com/contact-us
I assume this must be a problem with the .htaccess but I've tried various permutations and can't resolve it. I maybe wrong though, maybe it's something that ultimately needs to be fixed by a Laravel redirect after a url check.
It's driving me mad, I'd really appreciate any ideas!
Putting the entire application inside a public_html folder is a big red flag for security purposes. You have to be very very cautions with your permissions inside this folder. Things like .env, .git, cache files, private uploads, etc are usually downloadable by default and yes hackers will be scanning for treats.
If you can upload your entire app to the root of your cpanel host and rename public to public_html (or copy the contents of public into the existing public_html if you cannot remove it)
Then you may have to modify your laravel app index.php to use this as it's public path.
In index.php under :
$app = require_once __DIR__.'/../bootstrap/app.php';
add:
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
With this the standard .htaccess should work without changes.
Hope that helps.

Install Laravel on domain path

I have created a new Laravel project (v5.5) which is related to my main website. Due to SEO-technical considerations I want the link to be like this: <mainwebsite.com/laravel>.
Both, <mainwebsite.com> and <mainwebsite.com/laravel> are deployed to an individual server. An Application Load Balancer redirects the traffic either to the main website or the new Laravel project.
The problem right now is that the Laravel app doesn't know that <mainwebsite.com/laravel> must be seen as the project's root. (The route / must go to <mainwebsite.com/laravel> and not to <mainwebsite.com>.
I've tried to add Route::prefix('laravel')->group() ... to web.php, which does fix the routes, but then the app's public dir can't be accessed.
Using relative paths like "/css/app.css" or "/laravel/css/app.css" won't fix this.
Is there a better way to set this up or does anyone know how this must be done?
The following did the trick for me.
Change the APP_URL in the .env file to http://www.mainwebsite.com/laravel
Move the contents of the public folder into a new folder inside the public folder and give it the same name as the path behind the URL (so in this case 'laravel').
/public
--- /laravel
------ /css
------ /js
------ index.php
------ ... etc...
Edit the relative paths in the index.php file:
require __DIR__.'/../../vendor/autoload.php'; &
require_once __DIR__.'/../../bootstrap/app.php'; (add an extra /../)
Set the right paths in webpack.mix.js and add the following rules to the beginning of the file to make sure relative paths in your files will be rewritten to the right dir:
mix.setPublicPath('public/laravel/');
mix.setResourceRoot('/laravel/');
That's it!
If you're using apache, setup .htaccess to rewrite the url:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/laravel/
RewriteRule ^(.*)$ /laravel/$1 [L,R=301]
</IfModule>
This should rewrite all url paths that do not start with laravel to ones that do.
For nginx, you could do the following:
location ^~ /laravel(.*) {
return 301 $scheme://$http_host/laravel$1$is_args$query_string;
}

How to route request to codeigniter project outside document root

I have the following shared hosting file structure using a codeigniter project:
myTLD.com/sites/mysite
mysite contains: application, system , index.php ... ( standard CI2 setup )
myTLD.com/public_html - contains : index.php
I have symlinked myTLD.com/public_html/index.php to myTLD.com/sites/mysite/index.php
Unfortunately I am getting:
Your system folder path does not appear to be set correctly. Please open the following file and correct this: index.php
I have set it up this way to avoid placing the actual site in the document root for security purposes . I don't want to change mysite/index.php because I want to keep the entire project in its mysite directory where it can easily be revised etc.
The application and mysite/ folder are set to 755 so I don't think this is a permission problem .
My myTLD.com/public_html/.htaccess folder directs all requests to index.php:
RewriteEngine On
RewriteRule ^(.*)$ index.php
Can someone advise me on an approach to sending requests through to the codeigniter index file without causing this error?
Thank you
You can try following way
1) Remove the symlink
2) Use this in htaccess at myTLD.com/public_html/.htaccess
RewriteEngine on
RewriteRule ^(.*)$ myTLD.com/sites/mysite/index.php?/$1 [L]
Use absolute system path if you are aware of it.

how do i hide /magento/ in my site's url?

A site has an existing system (lets call it mysite)
and the client asks to put in magento.
My directory structure goes something like this:
ROOT
-index.php (this is the app's main controller)
-.htaccess
/blog (runs wordpress)
/assets (current system's media folder)
/magento (this is where all magento files go)
Problem is if I set up magento and specify in the installation that base URL is http://example.com, magento loads up mysite.
Leaves me no choice but to setup magento with base URL set to http://example.com/magento/ and it runs perfectly.
However the client wants me to feel hell and asks me to hide magento in the URL.
I’m not really versed in .htaccess and I know only simple rewrite codes so I tried forwarding any HTTP requests that start with /magento to the magento folder and came up with:
RewriteCond %{REQUEST_URI} !^/magento(.*)
RewriteRule (.*) /magento/$1 [L]
Just when I thought it was working, mysite links all became unaccessible and forwards to the magento system displaying it's 404 page.
So, uhm, can I ask for help how to construct the .htaccess to hide the /magento/ on the URLs without affecting the current system aka mysite?
Because you have existing applications off the webroot, you cannot get away with using nothing instead:
### webroot/.htaccess
RewriteRule ^whatiwanttouseinsteadofmagento/(.*)$ magento/$1 [L]
From how I see the problem you will not be able to hide magento completely and use your site as well in the same time.
If you want Magento in the root of the public folder you should just point the virtualHost to your magento installation but this will let your blog and your main controller out of the public view. This is more or less the same with what you did by redirecting all calls in the .htaccess to magento folder.
What I suggest is to change the magento name to something more anonymous like "shopping" or "cart", and remember that a folder rename is preferable to a .htaccess file in terms of security and performance.
Let's look at it:
RewriteCond %{REQUEST_URI} !^/magento(.*)
So we're saying the condition is anything that is not /magento(.*), so everything but that directory? This would redirect everything, including your blog, assets, and any other directories.
Without specifying each and every file that needs to be redirected to the magento directory, there really is no easy way of doing it. I suppose you could redirect any file that does not contain a "/" in it and ends with the extension .php to the magento directory. That way only files in the root web directory will redirect to magento, but if you used other directories inside the magento directory you'd still need to add separate rules for them.
this answer comes very late but I guess you wanted something like
RewriteCond %{REQUEST_URI} !^/(blog|assets|magento)(.*)$
RewriteRule ^(.*)$ /magento$1 [L]

Categories