Laravel root route "/" doesn't work inside prefixed routes on subdomain - php

I have the project with the following routes:
Route::namespace('Admin')->prefix('admin')->group(function () {
Route::group(['middleware' => 'auth:admin'], function () {
// this is working on my local machine but it's skipped on server which is a subdomain.
Route::get('/', 'DashboardController#index')->name('admin.dashboard');
// other routes here. all of them working fine
Route::resource('pages', 'PageController')->except(['show']);
});
});
Route::get('/', 'HomeController#index'); // this is working fine
On my local machine when I open app.test/admin everything work as expected. I can see my login page if I'm not logged in or the dashboard view if I am.
On the server, which is a subdomain, if I open subdomain.app.com/admin I see nothing. The server response is 200 without any error. All subsequent routes like admin/pages work fine.
The document root in apache is set to the root of the project, not inside public folder. I use the following .htaccess file in the root directory.
<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
</IfModule>
I expect the /admin route to show the dashboard view but I get a blank page. Where can be the issues when there is no error?

Does building your Routes list like this help at all?
Route::group(
[
'domain' => '{account}.app.test',
'prefix' => 'admin', // This is the URL prefix
'as' => 'admin.', // This gets pre-pended to the route names.
'middleware' => 'auth:admin',
'namespace' => 'Admin'
],
function () {
Route::get('/', 'DashboardController#index')->name('dashboard'); // with 'as' property, this becomes 'admin.dashboard'
Route::resource('pages', 'PageController')->except(['show']);
}
);

You can do this by setting your admin and user domains in your .env file and then using Route::domain like so:
Route::domain(env('APP_ADMIN_URL'))->group(function() {
Route::get('/', 'AdminController#index');
});
Route::domain(env('APP_USER_URL'))->group(function() {
Route::get('/', 'UserController#index');
});
No need to touch your htaccess other than to make sure that your both of your domains route to your laravel public directory

I found the answer ;)
The problem was that I had a folder called admin in my public folder wich was the same as the route I was trying to access. Renaming that folder to a random name fixed my /admin route.

Related

Laravel routes in Shared Hosting not works properly

I'm with a problem very similar with this another question, but the solutions provided in that thread not helped me.
I made the deploy of my Laravel app to a shared hosting service using this guide (BTW, the guide is very similar to one of the answers of the linked question)
Below is my routes/web.php file
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::group(['prefix' => 'law', 'middleware' => ['ability:superadministrator,read-users|create-users']], function () {
Route::get('/search', 'LawController#showSearchView');
Route::get('/new', ['middleware' => ['permission:create-users'], 'uses' => 'LawController#showNewView']);
Route::get('/find', 'LawController#find');
Route::get('/edit/{id}', 'LawController#edit');
//region post
Route::post('/new', ['middleware' => ['permission:create-users'], 'uses' => 'LawController#store']);
Route::post('/search', ['middleware' => ['permission:read-users'], 'uses' => 'LawController#search']);
/*Route::post('/modify', ['middleware' => ['permission:create-users'], 'uses' => 'LawController#modify']);*/
//endregion
});
Route::group(['prefix' => 'user', 'middleware' => ['ability:superadministrator,read-users|create-users']], function () {
//..similar to the previous one
});
//some other route groups
The issue is, I can access the welcome page and also the login page. After authentication, I go to the home page and then the problem starts. Every other route than I try (like mydomain.net/law/search) leaves me to the home page again in some kind of infinity loop. The logout route also works fine.
Whats is strange to is that if I try a inexistent route like mydomain.net/blah, I go the the knowledge RouteNotFound/"NotFoundHttpException" of Laravel Framework.
I don't receive any error in php log or in my browser console. The javascript works fine.
I contact the support of the host service to ensure that Apache had the mod_rewrite enabled and apparently it is.
I googling for it about 3 hours and dig deeper here in SO, but anything helped me to understand the problem. Any tip?
Below is the .htacces file if it helps in any way:
<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}]
</IfModule>
MISC:
PHP 7.1
Laravel 5.4
The composer.json for reference
I'm using the Laravel AdminLTE and the sidebar menu also does not work properly. I think that is the same problem causing the both issues.
My .env file does not have anything special, but here is (part of) it.
Add this in your .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [L]
This rule will internally load content of REQUEST_URI to the index.php

No Page Available - Laravel 5.2

I downloaded CMS project from github, Laravel version 5.2. I installed the composer in the folder and change the database info in .env file and the project working fine.
But only the home route is working, the rest of the routes giving me "Opps no page avaible"
The blades files are fine only the routes not working!
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController#index');
Route::get('/post/{id}', ['as' => 'home.post', 'uses' => 'AdminPostsController#post']);
Route::group(['middleware' => 'admin'], function () {
Route::get('/admin', ['as' => 'admin.index', function () {
return view('admin.index');
}]);
Route::resource('admin/users', 'AdminUsersController');
Route::resource('admin/posts', 'AdminPostsController');
Route::resource('admin/categories', 'AdminCategoriesController');
Route::resource('admin/medias', 'AdminMediasController');
Route::resource('admin/comments', 'PostCommentsController');
Route::resource('admin/comment/replies', 'CommentRepliesController');
});
Route::group(['middleware' => 'auth'], function () {
Route::post('comment/reply', 'CommentRepliesController#createReply');
});
and here is .htaccess file:
<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}]
</IfModule>
Because this route is defined before your resource route, your routes will not match the resource action resulting into "Opps! no page available".
I believe moving that route definition after your resource route definitions should resolve your issue.
Give it a try & let us know if this gets resolved.
As from your described routes, other then home routes are grouped with admin middleware.
Just confirm with your admin middleware about 404 redirection.
Update
Check your admin middleware and update it with below code:
public function handle($request, Closure $next)
{
if ((Auth::check()))
{
//Here, YOURADMINROLE replace with your actual admin role..
if ((Auth::user()->hasRole('YOURADMINROLE')))
{
return $next($request);
}
}
App::abort(403, 'Access denied');
}

Laravel 5 routing issue on redirecting url

I am new to laravel runing a project in it,i got error when i enter a module name to url NotFoundHttpException in RouteCollection.php line 161
And here is my routes.php file Code:
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', ['as' => 'home', 'uses' => 'Landing#index']);
Route::get('/buyerregistration',['as' => 'buyerregistration', function () {
return view('buyerregistration');
}]);
Route::get('owarehouse/{id}', ['as' => 'owarehouse',function () {
return view('owarehouse');
}]);
Route::get('/SMM', ['as' => 'SMM',function () {
return view('SMM');
}]);
Route::get('productconsumer/{id}/{openwish_id?}',array(
'as' => 'productconsumer',
'uses' => 'ProductController#productconsumer'));
I have .htacces file in my /public folder as:
<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>
Can anyone help me to sort out this,
Thanks
Enable Mod_rewrite in your Xampp/Wampp's Apache.
Go to {xampp_dir/wampp_dir}/apache/conf/httpd.conf and find the line
#LoadModule rewrite_module modules/mod_rewrite.so and remove the # before it.
Allow Apache to Overwrite the htaccess file from anywhere. Find the line
AllowOverride None and change it to AllowOverride All
Try localhost/Opensupermall/public/SMM
The code here points to your views after the public folder.
Now,the best practice would be in production to make your server's root folder to point the /public/ folder
Route::get('/SMM', ['as' => 'SMM',function () {
return view('SMM');
}]);
So you just type your route names after your root directory name.

Laravel group route issue

I'm using this code to implement admin area routes but Route::get('/',...) doesn't work, it seems I should use anything other than / in get ,otherwise laravel doesn't load view when I browse to mysite/admin/.
Route::group(['prefix' => 'admin', 'namespace' => 'admin', 'as' => 'admin'], function() {
Route::get('/', function() {
return view('backend.index');
});
Route::resource('post', 'PostController');
});
UPDATE: there is an admin folder in public that is public/admin. It seems Laravel open this directory instead of going through the route !
is it normal ? does public folder structure has priority to Route::get() ?
If you have admin folder inside public folder it's normal that this directory content will be displayed but it's not Laravel issue.
If you look in public/.htaccess you have there something like this:
<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>
so if directory or file exists in public directory Laravel will not launch application but server will display this directory or this file. This is what should be done, because if there wouldn't be such rule no CSS files, JavaScript files or images could be displayed.
What you should do is either change directory name in public folder from admin to something else (and then make changes in your code to reflect this change) or change admin route to something else

Laravel default route for subdirectory in a route group with prefix

I have a route group for the prefix admin. I want that if the URL http://www.example.com/admin/ is entered it by default loads the login page residing at http://www.example.com/admin/login. The login page is actually a controller, but I don't mind if the admin/ redirects to admin/login or routes to its controller directly. From other answers I saw here it seems that redirection is better to make sure links are not messed up.
I have tried various solutions with both routing and redirection, including the solution suggested here but I am alwas getting Error 404. What is the recommended proper way to achieve this?
My route group looks like this:
Route::group(array('prefix' => 'admin', 'namespace' => 'MyNamespace\Controllers\Admin'), function()
{
//the following work fine
Route::get('login', array('uses' => 'AdminLoginController#showLogin'));
Route::post('login', array('uses' => 'AdminLoginController#doLogin'));
Route::get('logout', array('uses' => 'AdminLoginController#doLogout'));
//other resource routes for the respective admin pages
});
Outside the route group I added the following, so that even http://www.example.com/admin without the trailing slash goes to the login page, which works fine.
Route::get('admin', function() { return Redirect::to("admin/login"); });
The problem is with http://www.example.com/admin/ that is giving Error 404. I tried all the following (separately obviously), and none works. All of them were inside the route group.
Route::get('/', function() { return Redirect::to("admin/login"); });
Route::get('', function() { return Redirect::to("admin/login"); });
Route::get('/', function() { return Redirect::to("login"); });
Route::get('', function() { return Redirect::to("login"); });
Route::get('/', array('uses' => 'AdminLoginController#showLogin'));
Route::get('', array('uses' => 'AdminLoginController#showLogin'));
I also tried this outside the route group:
Route::get('admin/', function() { return Redirect::to("admin/login"); });
None of them work. What is the right way to set a default route for a route group with a prefix subdirectory?
use this code in .htaccess
so your server will redirect url's with trailing slashes to url without.
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
# redirect everything to url without trailing slash
RewriteCond %{HTTPS} =on
RewriteRule ^(.+)$ - [env=ps:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.+)$ - [env=ps:http]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_METHOD} ^GET
RewriteRule ^(.+)/$ %{ENV:ps}://%{SERVER_NAME}/$1 [R=301,L]
# pretty urls
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
by default laravel firstly did redirection, but later was removed.

Categories