Forum Moderators: phranque
Options +FollowSymLinks -Indexes
DirectorySlash Off
RewriteEngine On
## Remove trailing slashes...
# If it's a directory
RewriteCond %{SCRIPT_FILENAME} -d [OR]
# or it's a PHP file.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# Redirect to remove the trailing slash.
RewriteRule ^(.+)/$ /$1 [R=301,L]
## Remove .php
# If it's a .php file
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
## Add .php.
# If a .php file exists
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*[^/])/?$ /$1.php [QSA,L]
RewriteRule ^(.*[^/])/?$ /subdirectory/$1.php [QSA,L] #unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.*)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.*)\.php$ $1 [R=301,L]
#resolve .php file for extensionless php urls
RewriteRule ^(([^/]+/)*([^/.]+))/$ /$1.php [L] RewriteEngine On
#
# Internally rewrite extensionless URL to corresponding .php
# file unless the URL exists as a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ $1.php [L]
#
# Externally redirect (only) direct client requests for .php URLs to extensionless URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.com/$1 [R=301,L]
^(([^/]+/)*[^.]+)$