如何使用htaccess使用或不使用尾部斜杠

I have htaccess in my PHP website. I want to work url with or without trailing slash.

Below code works only with the trailing slash.

Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteRule ^(.*)/$ $1.php [NC,L]

e.g. www.mydomain.com/index/ --Works
www.mydomain.com/index --Not Work

If anybody know how can I get this without trailing slash?

Thanks

You have this rule:

RewriteRule ^(.*)/$ $1.php [NC,L]

If you want to match it regardless of / in the end use:

ErrorDocument 404 /not-found.php

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

RewriteCond %{REQUEST_FILENAME} !-f will stop reapplication of same rule since $1.php will be a real/valid file.

Put a ? after the / to make it optional.

Fix would be

RewriteRule ^(.*)/?$ $1.php [NC,L]

This is what I did with something similar

RewriteRule ^(.*)/?$ /$1.php [NC,L]