please, I am trying to host Laravel 5 on a shared hosting.
When I access www.example.com/my_app_name, the page loaded correctly.
However, subfolders return 404 error. e.g. www.example.com/my_app_name/any_sub_folder or www.example.com/my_app_name/auth/login all return a 404 error.
shared_hosting_root
|--other_folders (not accessible via web domain)
|--applications (not accessible via web domain)
|--my_app_name
|--app
|--GoPublic.php <--I created this
|--bootstrap
|--config
|--database
|--public <--copied content to public_html
|--assets
|--index.php
|--resources
|--storage
|--tests
|--vendor
|--public_html
|--my_app_name
|--assets
|--index.php <-- I pointed here
I created a new file: shared_hosting_root\applications\my_app_name\app\GoPublic.php
<?php namespace App;
class GoPublic extends \Illuminate\Foundation\Application
{
/**
* Get the path to the public / web directory.
*
* #return string
*/
public function publicPath()
{
return $this->basePath.DIRECTORY_SEPARATOR.'../../public_html/my_app_name';
}
}
I edited bootstrap\app.php like so:
<?php
/* -- remove/comment this original code
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
-- until here */
/* -- add this new code -- */
$app = new App\GoPublic(
realpath(__DIR__.'/../')
);
I edited public\index.php like so:
I changed this:
require __DIR__.'/../bootstrap/autoload.php';
to
require __DIR__.'/../../applications/my_app_name/bootstrap/autoload.php';
and also changed this:
$app = require_once __DIR__.'/../bootstrap/app.php';
to
$app = require_once __DIR__.'/../../applications/my_app_name/bootstrap/app.php';
I then copied the content inside applications\my_app_name\public into public_html\my_app_name.
I followed the steps highlighted in this tutorial: http://blog.kongnir.com/2015/09/25/setting-up-laravel-5-on-shared-hosting-server/
The problem again:
I can access www.example.com/my_app_name.
But I get a 404 error on www.example.com/my_app_name/any_sub_folder or even a login redirect www.example.com/myapp_name/auth/login
What am I doing wrong? Help, please.
Thank you.
Update
www.example.com/my_app_name/sub_folder returns 500 internal server error
My .htaccess file in public_html directory:
# Do not change this line.
RewriteEngine on
# Change example.com to your domain name
# RewriteCond %{HTTP_HOST} ^(www.)?schoolsmart.com.ng$
# Change your_app_name to the subfolder name
# RewriteCond %{REQUEST_URI} !^/wbs/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change your_app_name to the subfolder name
# Change example.com to your domain name
# RewriteRule ^(.*)$ /wbs/$1
# RewriteCond %{HTTP_HOST} ^(www.)?schoolsmart.com.ng$
# RewriteRule ^(/)?$ wbs/index.php [L]
RewriteCond %{REQUEST_URI} !^wbs
RewriteRule ^(.*)$ wbs/$1 [L]
For me chmoding the bootstrap, storage and yajra folder which is under vendor directory to 775, as suggested in the official laravel documentation, did the trick.
Related
I'm in Laravel 5.8 in Windows trying to create an inbound API route. Postman always gives me a 404 when I try to hit this route:
http://MYHOSTISHERE/api/v1?event=NewDealerSetUp&api_key=MYKEYISHERE
CURL with
curl -X POST "http://rx-0-unicorn.local/api/v1?event=NewDealerSetUp&api_key=MYAPIKEYHERE"
also gives me a 404.
Can someone point me to what I'm doing wrong? I'm bald but I may start pulling out beard in a minute.
Details below, and thanks!
I have this route in app.php:
Route::post('v1?event={event}&api_key={api_key}', 'API\APIController#index');
The route shows in artisan route list:
| | POST | api/v1?event={event}&api_key={api_key} | | App\Http\Controllers\API\APIController#index | api |
VerifyCsrfToken Middleware (just to see if it would work):
protected $except = [
'api/*'
];
Controller start:
namespace App\Http\Controllers\API;
use App\Models\Log\LogAPI;
use App\Models\Members;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;
class APIController extends Controller
{
public function index($event, $api_key, Request $request)
{
$data = filter_var_array((array)$request, FILTER_SANITIZE_SPECIAL_CHARS);
[...]
if($event == 'NewDealerSetUp'){
$setup = new NewDealerSetup();
return $setup->newDealerSetup($request);
}
.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]
# ------------------------------------------------------------------------------------------------------
# NOTES:
# the log: strip per-dir prefix: C:/apache/htdocs/MYAPPNAME/public/ ->[EMPTY] ... takes the ^[EMPTY] and returns an empty
# ------------------------------------------------------------------------------------------------------
</IfModule>
Your route definition is the issue here. You've used query parameters in the route definition v1?event={event}&api_key={api_key} which is wrong. I'm not sure of it, but I've never seen any route definitions like this before as in route definition, we usually only define the endpoint and don't give any query params
Your controller definition is
public function index($event, $api_key, Request $request)
For this to work, your route would need to be something like
Route::post('v1/{event}/{api_key}', 'API\APIController#index');
or anything similar like
Route::post('v1/event/{event}/api_key/{api_key}', 'API\APIController#index');
If you must use query parameters, then you will have to use
Route::post('v1', 'API\APIController#index');
and handle the params in your controller
public function index(Request $request)
{
$event = $request->event;
$api_key = $request->api_key;
// Rest of code
}
This is the only thing that i can see on my computer at the address http://localhost/onlineExam/login/validate_credentials
Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.39 (Win64) OpenSSL/1.1.1c PHP/7.3.7
class Login extends CI_Controller
{
function index()
{
$data['main_content'] = 'login_form';
$this->load->view('includes/templates', $data);
}
function validate_credentials(){
$this->load->model('user_model');
$query = $this->user_model->validate();
if($query)// if the user's credentials validated...
{
$data = array(
'username' => $this->input->post('username'), 'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('site/users_area');
}
else{
$this->index();
$data['error'] = 'Invalid Username or Password';
$data['main_content'] = 'login_form';
$this->load->view('includes/templates', $data);
}
}
}
Please check .htaccess file is present in the root folder, that is 'onlineExam' as per your URL.
If not present, create/update with this lines:
RewriteEngine On
RewriteBase /onlineExam/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
After that, also check base_url at onlineExam/application/config/config.php
$config['base_url'] = 'http://localhost/onlineExam';
This could be a number of things
1) As Stated above you need to make sure a htaccess file is present in the root of your project folder
2) If your htaccess file isn't removing the index.php part of your URL then your link would not be
http://localhost/onlineExam/login/validate_credentials
but rather
http://localhost/onlineExam/index.php/login/validate_credentials
3) Make sure your filename is the same as your class name. At times too case sensitive operating system may affect your files. Login.php != login.php (This is a rare case and happened to older version of some OS')
I usually use something like:
class User extends CI_Controller {
public function save() {
if($this->input->is_post()) { //my own method
......
}
}
}
Is there any other way, eg. in Slim framework:
post("/user/save", function() {
......
});
or in .Net MVC:
[HttpPost]
public ActionResult save(User model) {
......
}
Or can CodeIgniter handle this in its route config file?
Thanks for answer.
Codeigniter has no built-in support for REST. If you want it, you need to use third-party library or write your own. For third-party library, Here is good one : codeigniter-restserver .
Hope it will be useful for you.
To remove index.php use this in your .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I want user redirect to https insetad of http just after login in my new website made in zend framework2. please help.
I there any method available in zend freamework2 to get current used protocol(http or https).
You can redirect the user to the https version of a page inside the application. Above solution simply redirects all requests to it's http variant, and only for Apache. I would rather do it inside the application (then you're more in control) and make it server agnostic (what happens when you switch to nginx?)
An example is based on an answer I gave earlier here: ZF2 and force HTTPS for specific routes
The solution: create a controller plugin which you can call every time you need it.
Specify the pages you need to force a redirect (i.e. the page you show the login form):
use Zend\Http\Response;
public function loginAction()
{
// If return value is response, this means the user will be redirected
$result = $this->forceHttps();
if ($result instanceof Response) {
return $result;
}
// code here
}
And then you could create a controller plugin (call it ForceHttps) to return a response when the user should be redirected:
use Zend\Uri\Http as HttpUri;
class ForceHttps extends AbstractPlugin
{
public function __invoke()
{
$request = $this->getController()->getRequest();
if ('https' === $request->getUri()->getScheme()) {
return;
}
// Not secure, create full url
$plugin = $this->getController()->url();
$string = $plugin->fromRoute(null, array(), array(
'force_canonical' => true,
), true);
$url = new HttpUri($string);
$url->setScheme('https');
return $this->getController()->redirect()->toUrl($url);
}
}
What happens is
Check if the current scheme is https. If not, continue
Grab the current used url from the url() plugin, use the current route name (hence the first parameter is null), force the canonical version (i.e. with a full scheme and domain name) and reuse the current route match parameters (the true as last parameter).
Import the url (as a string) in the Zend\Url\Http object
Reset the scheme to the https version
Pass the url to the redirect plugin to grab a response which will perform the redirect
on your .htaccess write these rules
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ public/index.php [NC,L]
note that public/index.php is the root so you have to modify it and www.yoursite.com too
for logged-in user you have to do with php
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity()) {//redirection should be here//}
my problem is about the route of codeigniter, I'm working with that and for each function i wrote the route. When i put the last route, this one not working and it give me 404 error. I don't know why? maybe i'm doing mistake about the order of route but i think is right.
This is my route:
//MATCH SHARE
// route not working
$routes['upload_image/upload_photo'] = "upload_image/upload_photo";
//all route working
//VOTATION//
$route['auth_social/fblogin'] = "auth_social/fblogin";
$route['votation/user_plus/(:any)'] = "votation/user_plus";
$route['votation/user_minum/(:any)'] = "votation/user_minum";
$route['finish_registration/(:any)/(:any)'] = "auth/activate"; // activate e-mail
$route['auth'] = "auth"; // index auth
$route['register'] = "auth/register";
$route['login'] = "auth/login"; // login
$route['logout'] = "auth/logout";
$route['default_controller'] = "auth/register"; // register
$route['(:any)'] = "profile/user";
$route['404_override'] = '';
htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Try:
$routes['upload_image/upload_photo/?'] = "upload_image/upload_photo";
$routes['upload_image/upload_photo/(.+)'] = "upload_image/upload_photo/$1";
If you need to have extra uri segments, your current one won't work.
Your variable is wrong $routes it should be $route['upload_image/upload_photo'] = "upload_image/upload_photo";