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!

4
  • I asked if this was a dupe, 12 mins ago. Since you didn't answer, I guess you accepted that this was a dupe. Commented Jun 7, 2017 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" Commented Jun 7, 2017 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. Commented Jun 7, 2017 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. Commented Jun 7, 2017 at 14:56

1 Answer 1

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.