0

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!

Andrew T
  • 367
  • 2
  • 14
  • I asked if this was a dupe, 12 mins ago. Since you didn't answer, I guess you accepted that this was a dupe. – Praveen Kumar Purushothaman Jun 07 '17 at 14:21
  • Never saw (and still don't see) that question if it's a dupe. What you referenced as the dupe has the code I already put in above and as stated in the original question, that solves 1 of my 3 questions. It doesn't address the other 2... "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" – Andrew T Jun 07 '17 at 14:39
  • Trying some of the other answers there does help me along and solve some of the other issues so thanks for that. It is appreciated. I still haven't found the right combo but I guess I will just continue to guess and test based on that answer and the other linked ones. Is there a proper way to post a question to get help learning and/or resources instead of just the code? I'd like to actually learn they why better but not sure how to ask for that type of help. Thx. – Andrew T Jun 07 '17 at 14:45
  • There are some helpful answers in that thread but it is not exact duplicate since OP's requirements are a bit more complex. – anubhava Jun 07 '17 at 14:56

1 Answers1

2

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643