简体   繁体   中英

.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!

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM