darkcyber's avatar

URL with trailing slashes gets redirected to localhost

I can access my url with http://localhost/rnd/public/coeg-admin but if I visit http://localhost/rnd/public/coeg-admin/ it gets redirected to http://localhost/coeg-admin.

I just follow http://stackoverflow.com/questions/21735527/laravel-trailing-slashes-redirect-to-localhost and http://stackoverflow.com/questions/30775997/url-with-trailing-slashes-gets-redirected-to-localhost-in-laravel-5

But it's doesn't work as expected, Here is my .htaccess

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

    RewriteEngine On

    # Set base URL
    # RewriteBase /rnd/public/
    # RewriteRule ^(.*)/$ /$1 [L,R=301]
    # RewriteCond %{REQUEST_URI} !^

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

Here is my apache2.conf

<Directory "/var/www/html/rnd/public">
        Options Indexes FollowSymLinks
        #AllowOverride None
        AllowOverride All
        Require all granted
</Directory>

<Directory "/var/www/html">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

How, I can redirect url with slashes at the end to url without slashes at the end ?

Thanks, any help appreciated.

0 likes
5 replies
phildawson's avatar

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 you don't want to be prepending a slash to the result.

But really your DocumentRoot should be /var/www/html/rnd/public not /var/www/html

Snapey's avatar

you have multiple directory statements in your Apache config. This needs tidying up. better, learn how to use vurtualhosts and you will be able to serve several projects at once.

darkcyber's avatar

@Snapey Which one is multiple directory? then how to fix it?

I am currently in development mode @ localhost, so i need acces to other folder for testing purpose.

Snapey's avatar

uncomment the virtualhost directive and set as;

<VirtualHost *:80>
    DocumentRoot "/var/www/html/rnd/public"
    ServerName rnd.local
    ServerAlias rnd.local
    <Directory "/var/www/html/rnd/public/">
     Options All
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

restart apache

add an entry to your hosts file;

127.0.0.1    rnd.local

Check your routes.php and make sure that all your routes don't have any part of the server name in them, they are just plain as you would expect as routes within your site

revert your .htaccess file

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

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Visit rnd.local in your browser - hopefully it should be ok.

If you need another site, duplicate the virtualhost section changing the paths and the server name. Add new name in hosts.

Please or to participate in this conversation.