Search code examples
regexapache.htaccessmod-rewriteurl-rewriting

.htaccess remove .php but add a / at the end


I currently have some code added to my .htaccess file and it works as intended.

RewriteEngine On

# hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

For example I have = http://www.hello.com/world.php

This code removed the php and will look like this http://www.hello.com/world

I want it to look like this = http://www.hello.com/world/

How do I add the / to the end?


Solution

  • You can use this:

    RewriteEngine On
    
    # hide .php extension snippet
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1/ [R,L]
    
    # To internally forward /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]