deroccha
deroccha

Reputation: 1183

htaccess trailing slashes just for one URL

Having the following .htaccess rules

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

    RewriteEngine On
    RewriteRule ^dashboard/(.*)$ back/index.html [L]
    # 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>

I want to redirect all incomings requests if they match /dashboard to end with a trailing slash. But only for this case.

if I use

RewriteRule ^dashboard/(.*)$ back/index.html [L]
RewriteCond %{REQUEST_URI}dashboard /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

will fail because all my URL's are becoming a trailing slash. How can I have a trailing slash only for this case?

Upvotes: 2

Views: 53

Answers (1)

anubhava
anubhava

Reputation: 784868

Have it this way:

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

    RewriteEngine On
    RewriteRule ^dashboard/(.*)$ back/index.html [L]
    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # add / after /dashboard
    RewriteRule ^dashboard$ $0/ [L,R=301,NC]

    # remove / from all other URIs
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule !^dashboard/ %1 [L,R=301,NC]

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

Make sure to test it after completely clearing browser cache.

Upvotes: 1

Related Questions