Angular + Laravel PHP Routing in.htaccess

Good time of day.

Have some trouble.

There is a website in Angular and a server in PHP (Laravel).

The customer asked to remove the # from the url, after that, when updating the page, we catch 404.

RouterModule.forRoot(appRoutes, { useHash: false }),

I looked at how this is solved (Using the file configuration method .htaccess), but it did not help, because there are conflicts with PHP.

<IfModule mod_rewrite.c>
  <IfModule mod_negotiation.c>
      Options -MultiViews -Indexes
  </IfModule>

RewriteEngine On

# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

# 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]

Files index.html and index.php they are located in the same directory - /var/www/satu/public Can I somehow solve this the problem is solved via a file .htaccess, without changing the location of the files, or do you need to do something else?

Thanks.

Routes/web.php

<?php

Route::group(['prefix' => 'admin'], function () {
    Voyager::routes();
});

// Route for parse CSV files to DB
Route::get('/parseData', 'ParseController@parse');
Route::get('/parseImages', 'ParseController@addImages');

Route::get('/login', 'Web\LoginController@loginForm')->name('loginForm');
Route::post('/login', 'Web\LoginController@login')->name('login');
Route::get('/logout', 'Web\LoginController@logout')->name('logout');

Route::get('/register', 'Web\LoginController@registerForm')->name('registerForm');
Route::post('/register', 'Web\LoginController@register')->name('register');
Route::post('/check-phone', 'Web\LoginController@checkPhone')->name('check_phone');

Route::group(['middleware' => ['auth']], function() {

});

Route::group(['prefix' => 'api/v1.0', 'namespace' => 'Api\v1_0', 'middleware' => ['cors']], function () {
    Route::post('/login', 'LoginController@login');
    Route::post('/web/login', 'LoginController@webLogin');
    Route::post('/register', 'LoginController@register');
    Route::post('/confirm-phone', 'LoginController@confirmPhone');
    Route::post('/resend-confirm-sms', 'LoginController@resendConfirmSms');
    Route::post('/recovery-code-send', 'LoginController@sendRecoveryPasswordCode');
    Route::post('/recovery-password', 'LoginController@recoveryPassword');

    Route::group(['middleware' => ['auth']], function() {
        Route::get('/user', 'LoginController@user')->name('api.user');
        Route::put('/user', 'LoginController@userUpdate')->name('api.user.update');
        Route::post('/contracts/verify', 'PaymentsScheduleController@verify');
        Route::get('/notifications', 'NotificationController@notificationsList');
        Route::put('/user/token', 'LoginController@updateToken');
    });
});

Route::get('/api/soap/logs', 'Web\LogsController@showLogs')->name('admin.soap.logs');
Route::get('/api/soap/fix', 'Web\LogsController@fixLogs')->name('admin.soap.fix');

Route::get('/postman', function () {
    return view('welcome');
})->name('index');
Author: Demonmerc, 2020-01-18

2 answers

This is a SPA with routing via the history api. I won't say anything about the front, but I don't need to touch the back .htacess. You need to configure routes in laravel.

Specifically, laravel should have a route that accepts any url that returns the SPA application initialization page.

Unfortunately, you did not specify the laravel version, so we will accept the 5.5.* - 6.* version in the routes/web.php file. Instead of the / mashrut, we need to create a marshut like this (at the very bottom)

Route::get('{url}', 'Controller@enty')->where('url', '.*');
 2
Author: Максим К, 2020-01-19 14:11:00

Urls are built with a hash not on the laravel side, but on the angular side.

You need to change the angular LocationStrategy to PathLocationStrategy

 0
Author: k0mar, 2020-01-21 12:00:42