Skip to content
Advertisement

Laravel page redirection issue

Hi I am laravel beginner and having problem during page navigation.

http://localhost/mylaravel/public/index

In index page I have few items and below href to see the details of that item based on item id.*

<a href="{{route('singleitem',$product->id}}">
Route::get('/single/{id}', 'ProductController@single')->name('singleitem');
Route::get('/index', 'ProductController@index'); 

Now when I click on item details, I get the below URL which is ok.

http://localhost/mylaravel/public/single/25T8C    (here id = 25T8C)

Now from that page when I am clicking Home it’s adding index page end of single [single/index !!!]

<a href="index" class="nav-link">Home</a>
http://localhost/laravel-test/mylaravel/public/single/index

I tried to add a slash but it’s redirected me to

<a href="/index" class="nav-link">Home</a>
http://localhost/index

Here is my .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]
</IfModule>

Any help will be appreciated.

Advertisement

Answer

On live it is just required to pass /index

<a href="/index" class="nav-link">Home</a>

and it will redirect you to

http://yourdomain.com/index

but on the localhost, you have to add the directory name also(Note: on live there will be no folder name. So /index will work there).

<a href="/laravel-test/index" class="nav-link">Home</a>

Or, you have to run laravel through artisan server, then /index will work on your localhost also.

You are facing this issue because when we give / before URL, it concatenates the given URL with the host and on XAMPP we have to concatenate folder name after localhost.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement