user1154435
user1154435

Reputation: 165

htaccess: Remove and disable file ending

I'd plastered all over the Internet that this code in your .htaccess file will remove the file ending:

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

However, although enables the URL without file ending, it does not disable the URL with file ending.

Essentially, what I'd like to force any attempt for an request with file ending (domain.com/sample.php) to end up without file ending (domain.com/sample)

I'm sure someone will have covered this somewhere, but I cannot find anything.

Many thanks.

Upvotes: 2

Views: 219

Answers (1)

anubhava
anubhava

Reputation: 785108

Add this extra rule before your existing rule:

RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

Upvotes: 2

Related Questions