How to Setup Laravel5.1 App on SharedHost? - .htaccess

I'm trying to make my laravel5.1 application live on a sharedhost but I'm failing at it no matter how hard I'm trying.
my host structure is given at this image.
The laravel application is under app folder and the content of public folder of laravel cutted to public_html.
at the index.php of public_html I have :
<?php
/**
* Laravel - A PHP Framework For Web Artisans
*
* #package Laravel
* #author Taylor Otwell <taylorotwell#gmail.com>
*/
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels nice to relax.
|
*/
require __DIR__.'/../app/bootstrap/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../app/bootstrap/app.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request
| through the kernel, and send the associated response back to
| the client's browser allowing them to enjoy the creative
| and wonderful application we have prepared for them.
|
*/
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
Now I'm not seeing neither my index page nor watching any errors in /app/storage/logs/laravel.log .
by the way my .htaccess in public_html content is :
<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]
</IfModule>

Create folder laravel in your root directory copy all data to that folder except public folder
then copy all data from public folder to folder pubic_html
now edit your index.php in your public_html folder like this:
require __DIR__.'/../laravel/bootstrap/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
Then change Config/database.php and insert valid data for database connection
Good tutorial

Although the answer from Adnan can work. Is important to keep in mind that Laravel was not developed to work on a shared host.
Uploading the application folder in the root directory will expose also the configuration files.
This is a serious security problem and should be avoided!
Only the content of the 'public' folder (or/and resources if the view is there) should be in the root directory. All the other files must be placed outside the web root.
I also am aware that this is not so easy to do as seems to be.
But nowadays the security of your ad your customers data is more important than that!
That said, you can do of course whatever you want.

Related

How to remove public? from url in php project

I am currently working to build a small php mvc framework. in a framework i have a this folder structure.
-app
--controllers
-Post.php
-core
-logs
-public
--.htaccess
-- index.php
-vendor
in here index.php is working as Front Controller
in post controller is look like this..
<?php
/**
* Posts controller
*
*/
class Posts
{
public function index()
{
echo 'Hello index';
}
public function addNew()
{
echo 'Hello addNew';
}
}
in url, i want to remove project/public/?posts/index public/?. When i remove (public/?) and visit the url. its showing me this error message.
project/posts/index
The requested URL was not found on this server.
using public/? project/public/?posts/index is working fine. and its echo index message
project/public/
The .htaccess inside of the public folder contains:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
in project main root folder ...
i did't added .htaccess and index.php file.
in .htaccess when i add this line. url redirect to xammp welcome screen
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
I'd say you want to internally rewrite all incoming requests to the controller inside the /project/public folder. But that is not what you do. The rule you implemented (RewriteRule ^(.*)$ index.php?$1 [L,QSA]) only rewrites relative to the requested folder. No mentioning of "public" in there.
The actual setup you need depends a bit on your http host setup here. Where its DOCUMENT_ROOT points to. Most likely to the folder that contains the file system structure you posted in your question. If so you should implement a rule that rewrites all incoming requests to the /project/public folder.
Something like that, though you probably need to tweak it to match your actual setup:
RewriteEngine on
RewriteRule ^ /public/index.php?%{REQUEST_URI} [L,QSA]
You can implement such rule in the http server's host configuration. Or, if you do not have access to that, you can use a distributed configuration file (if you have enabled those for the http host), so a ".htaccess" style file. That file should be located inside the folder your http hosts DOCUMENT_ROOT setting points to. So the folder containing the file system structure your posted.
Other setups are possible, this is just one option. The point is: you need to rewrite the requests to your controller. Where the controller actually is.

redirect all requests to subdomain to an appropriate folder

I have multiple clients that I have subdomains for
client1.example.com client2.example.com ... etc etc
now in my root directory I have one common code base and rest is different depending on clients
so in my root directory I have these folders
mymainrepo client1 client2
mymainrepo contains all the common code
client1 contains multiple folders like cache dbsettings
client2 contains multiple folders like cache dbsettings
But the content of cache and dbsettings is different for every client.
so what I want to do is if a request comes in for a client and it asks for a cache folder contenet, i want it to be served form that clients folder
so if a request is client1.example.com/cache
it will serve the contents from
doc root
|
|
-------client1
|
|_______cache
and if the request is client2.example.com/cache
it will serve from
doc root
|
|
-------client2
|
|_______cache
and so on and so on.
Any help will be appreciated
thanks
** EDITED **
ok so I tried this and this sorta works
RewriteEngine on
RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.domain.com$[nc]
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [r=301,nc]
DirectoryIndex client1/index.html
problem is I want to have this dynamic instead of client1 hard coded
DirectoryIndex client1/index.html
I tried
DirectoryIndex %1/index.html
but that did not work
any help will be appreciated
You can use something like the following
RewriteEngine on
RewriteCond %{HTTP_HOST} ^((?!www\.).+)\.domain.com$
RewriteRule ^.*$ /%1%{REQUEST_URI} [L]
This will rewrite client.domain.com/path to the /client folder of your main document root. So if the request is client.domain.com/cache this will internally redirect it to /client/cache showing you the contents from that folder.

Silex: reset the root route when the app is not at the webroot level

I am playing with Silex, trying to use it as a RESTful json api at a shared web hosting. The host has Apache web server. I would like the Silex app to sit in the folder which I tentatively called experiments/api, so the app becomes at a different level than the webroot. As per the documentation, the .htaccess file that I placed in the Silex app folder looks like this:
RewriteEngine On
RewriteBase /experiments/api
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ src/index.php [QSA,L]
(meaning that the app sits in the /experiments/api folder, and the main controller file is in src folder and is called index.php)
This gets the job done (i.e. the requests to /experiments/api/ are picked up by the Silex app), but the inconvenience is that the app now sees this /experiments/api/ prefix of the pathname.
For example. When I send a GET request to /experiments/api/hello I want the app to ignore the /experiments/api part, and to match only the /hello route. But currently the app tries to match this whole /experiments/api/hello path.
Is there a way to reset the root route for Silex to include the constant part of the path? I looked through the docs, but couldn’t find the answer.
You could use the mount feature.
Here's a quick and dirty example:
<?php
// when you define your controllers, instead of using the $app instance
// use an instance of a controllers_factory service
$app_routes = $app['controllers_factory'];
$app_routes->get('/', function(Application $app) {
return "this is the homepage";
})
->bind('home');
$app_routes->get('/somewhere/{someparameter}', function($someparameter) use ($app) {
return "this is /somewhere/" . $someparameter;
})
->bind('somewhere');
// notice the lack of / at the end of /experiments/api
$app->mount('/experiments/api', $app_routes);
//...

CakePHP and AngularJs in routing harmony: htaccess rules to send user to index.html or bootstrap index.php

I'm building an app with CakePHP as my backend and AngularJS as my front-end framework.
I need to have CakePHP on the same server as my front-end code so it can serve the JSON I need.
Typically in CakePHP, one has the webroot folder, and besides the js/css/less/*, there is an index.php that hooks to CakePHP. Adding AngularJS, though, adds some complexity because I also want an index.html.
Here is the control flow I'd like to have:
If the user goes to domain.com/, display index.html (but the route stays '*.com/'!)
If the user goes to domain.com/#/home, display index.html and let Angular take care of the hash route
If the user goes to domain.com/about (assuming the about page, html and all, is served by CakePHP), send them to index.php.
If I make a AJAX call with domain.com/users/list.json, use index.php so that CakePHP can serve the JSON.
So my thought was I could have .htaccess rules take care of this. But I'm not quite sure what to write.
Right now I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d #if it's not an existing directory
RewriteCond %{REQUEST_FILENAME} !-f #if it's not an existing file
RewriteRule ^(.*)$ index.php?/$1 [QSA,L] #everything else, send to CakePHP
</IfModule>
Any ideas?
Would it be as simple as adding a line after the last condition like RewriteRule / index.html [L]?
Also, please feel free to suggest alternative setups with CakePHP and AngularJS.
Thanks!
EDIT: I want to give more information on the build system I'm using for AngularJS and why it poses issues with just serving from the 'home.ctp' view.
I'm using ng-boilerplate which uses karma for testing and grunt for concatenation, uglifying, and including scripts in index.html.
I think the build system is truly beautiful since it allows me to keep a modular structure.
Here is the directory structure for a typical ng-boilerplate front-end:
ng-boilerplate/
|- build/ # Development build
| | index.html
| | assets/
| |
| |- angular/
|- grunt-tasks/
|- karma/
|- src/ # Actual source code I'm editing
| |- app/
| | |- <app logic>
| |- assets/
| | |- <static files>
| |- common/
| | |- <reusable code>
| |- less/
| | |- main.less
|- vendor/
| |- angular-bootstrap/
| |- bootstrap/
| |- placeholders/
|- build.config.js # Build configuration specific to app
|- Gruntfile.js
|- module.prefix
|- module.suffix
|- package.json
(More info on each of these directories and folders here: https://github.com/joshdmiller/ng-boilerplate)
This makes for a difficult workflow, since I have to take the most recent 'build' (during development) and place the index.html file manually in home.ctp every time I edit index.html.
This is why my mind first went to .htaccess. Though, I understand that this idea has its own limitations if I ever do want to serve a page from CakePHP directly rather than bringing AngularJS into the picture.
Maybe I could configure the build system to place the index.html in the CakePHP's home.ctp view. Any ideas on this solution?
Keep it simple
One of Cake's main principles is KISS ("Keep it Simple (Stupid)").
You don't need any rewrite rule changes if the response for the url / is that of your index.html file
i.e. do the equivalent of this:
cd app
mv webroot/index.html View/Pages/home.ctp
<edit View/Pages/home.ctp>
If you put the following at the top of the view file:
<?php $this->layout = false; ?>
You can also leave the contents as the complete raw html file.
Doing this would remove the need for any .htaccess modifications.
A specific note about one of the bullet points:
If the user goes to domain.com/#/home, display index.html and let Angular take care of the hash route
Nothing needs doing for that, as a url fragment is not sent by browsers with requests - the url received on the server is domain.com/.
But if you really want to do it with mod rewrite only
Then all that's required is to add a rule for the url / - which is the only exception:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /index.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Be sure to use the .htaccess rules for the exact version of cake you're using as they changed over the years and can cause odd behavior using an older version of the rewrite rule with a newer version of Cake.
Note that the directory-exclusion condition isn't required and is probably not desired (it allows a directory listing with e.g. /css).
view link:
Bake backend REST API
I will reuse the database scheme of the CakePHP Blog tutorial.
Prepare posts table
Execute the following scripts to create a posts table in your MySQL database, it also initialized the sample data.
http://hantsy.blogspot.com.ar/2013/11/create-restful-application-with.html

Getting Symfony2 to work on shared hosting using only .htaccess

I've been searching for this for 4 days now and couldn't find a working solution.
I want to make Symfony2 work on shared hosting without access to command line or httpd.conf (there's no way to set virtual host). All I can do, is just edit .htaccess files. In my web root directory I also have some other projects (like forum). The directory structure is:
public_html
|-forum
|-ox
'-Symfony
|-app
|-bin
<...>
I can make it work both in dev ant prod environments (routing works well), BUT it doesn't load any assets (js, css, images). In error log there's always the same:
request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /bundles/acmedemo/images/welcome-demo.gif" (uncaught exception)
Same happens if asset is loaded not from bundles, but also in twig as:
{{ asset('css/main.css') }}
Then it ends up with
request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /css/main.css" (uncaught exception)
My .htaccess in public_html is:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# DEV ENVIRONMENT #
RewriteRule ^$ Symfony/web/app_dev.php [QSA]
RewriteRule ^(.*)$ Symfony/web/app_dev.php/$1 [QSA,L]
# PROD ENVIRONMENT #
#RewriteRule ^$ Symfony/web/app.php [QSA]
#RewriteRule ^(.*)$ Symfony/web/app.php/$1 [QSA,L]
Any suggestions how to make things right?
Interesting problem. After digging around the code I found following solution.
Create a class named PathPackage.php in src/Vendor/YourBundle/Templating/Asset folder with following code.
<?php
namespace Vendor\YourBundle\Templating\Asset;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Templating\Asset\PathPackage as BasePathPackage;
class PathPackage extends BasePathPackage
{
/**
* Constructor.
*
* #param Request $request The current request
* #param string $version The version
* #param string $format The version format
*/
public function __construct(Request $request, $version = null, $format = null)
{
parent::__construct("/Symfony", $version, $format);
}
}
Then in your app/config/config.yml add the following parameter.
parameters:
// ...
templating.asset.path_package.class: Vendor\YourBundle\Templating\Asset\PathPackage
Now it will append /Symfony to the asset url parameter.
To summarize asset twig function calls getUrl method to determine the url. Which is extended by this class. Object of the class is passed as argument during templating.helper.assets service creation. Luckily PathPackage class is configurable. So solution was possible :).
Do php app/console assets:install ./web locally, and upload the content of web folder your remote shared hosting.

Resources