1

I hit my url with http://localhost/project/women-fashion and its working fine but if I visit http://localhost/project/women-fashion/ it gets redirected to http://localhost/women-fashion.

My route:

Route::get('/{slug}/', 'SlugController@index')->where('slug', '[A-Za-z-0-9]+');

My htaccess:

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

How, I can redirect url with slashes at the end to url without slashes at the end ? Thanks, any help appreciated.

okconfused
  • 3,567
  • 4
  • 25
  • 38

1 Answers1

1

I got my answer. I have just added RewriteBase /project/ and remove / before the substitution RewriteRule ^(.*)/$ $1 [L,R=301]

The regex says capture (.*) everything from the start ^ to the end before the slash /$ and substitute it with what it found.

Basically I don't want to be append slash to the result.

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

        RewriteEngine On
        RewriteBase /project/

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

Reference post: https://laracasts.com/discuss/channels/laravel/url-with-trailing-slashes-gets-redirected-to-localhost

okconfused
  • 3,567
  • 4
  • 25
  • 38