Search code examples
.htaccessurlfile-extensiontrailing-slash

Issue when using .htaccess file to hide .php extension


I have the following .htaccess file on my website for removing extensions:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

This works perfectly on localhost, however, when I upload the file to GoDaddy and try to visit a URL, say mysite.com/page, Google Chrome automatically adds a trailing slash to the URL (just on the website, not on localhost). I'm assuming this causes the site to assume that page is a folder/directory and not a file? Is this what's happening? If so, what would be the best way to tackle the issue? Should I add to my .htaccess to automatically remove trailing slashes, or should I maybe have each page as a directory each with an index.php? What's the most efficient method?


Solution

  • It is possible to enforce no trailing slash if it is not a folder.

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R]