Mohamed Ebrahim
Mohamed Ebrahim

Reputation: 232

.htaccess - hidding .php and forcing trailing slash not working

I'm trying to hide .php extension from my website while forcing trailing slash, I've been searching for the last few days with no success.

The site is running in a XAMPP server currently.

Here's the .htaccess file:

Options -Indexes -Multiviews +FollowSymlinks

RewriteEngine On
RewriteBase /

#removing .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)/$ $1.php [L]

#forcing trailing slash
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]

ErrorDocument 404 http://localhost/404/

These links work:

localhost/about-us redirects to localhost/about-us/ which is correct but when I try to access localhost/about-us.php, it redirects to localhost/about-us/ instead of localhost/about-us/. (sorry can't post links)

How can this be fixed?

Upvotes: 0

Views: 68

Answers (1)

Anonymous
Anonymous

Reputation: 12017

You just seem to have accidentally used an absolute path. Just remove the slash at the beginning of /$1/ like your other rules to make it work properly.

#removing .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ $1/ [L,R=301]

Upvotes: 1

Related Questions