Andrew T
Andrew T

Reputation: 357

.htaccess Removing .php from URL and add trailing slash

I have a filesystem of PHP files and folder and want to make the URLs nicer but can't seem to find the right combo. This works to make the admin.php work as just admin but I can't find a good way to add the trailing slash and redirect it to the pretty URL if someone hits the PHP URL.

#Remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]

Wanted:

domain/admin.php => domain/admin/
domain/admin     => domain/admin/

Virtual and Real subfolders too?

 domain/folder/   => loads index.php if exists otherwise loads folder.php in root

If anyone has any tips I'd appreciate it!

Thanks!

Upvotes: 0

Views: 190

Answers (1)

anubhava
anubhava

Reputation: 784998

You can solve all 3 tasks using these 2 rewrite rules in your site root .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]

RewriteCond %{DOCUMENT_ROOT}/$1/index.php !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Upvotes: 2

Related Questions