Search code examples
.htaccesstrailing-slashtrailingtrailing-return-type

Redirect all pages with trailing to the URL without trailing WHEN part of the root is a folder


I have tried all codes found to redirect my pages when typing them with trailing to the root URL, BUT it works only for URL which are not folders, i.e. for a link /mylink.html I have the folder "mylink" and inside it I put my articles, so I try to visit the article and it performs well, BUT whenever I click on the folder with the same name as the file containing the sub-pages, it cracks to a 401 or 500 error!

Thank you in advance!

Here is my code:

# redirect trailing at the end
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpg|png|ico|webp|jpeg|css|js)$ [NC]
RewriteRule ^(.*)$ https://example.com/$1/ [L,R=301]

Solution

  • The answer flashed me just after posting the question... If this would help others, simply add this line for each file which "doubles" the name of a folder:

    RewriteRule ^mylink\/$ "https\:\/\/www\.website\-whatever\.com\/mylink" [R=301,L]
    

    Cheers ☺