301重定向也被重写的URL

I am building a site using Slim, which suggests the following .htaccess code for creating pretty URLs:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

This means that a URL like http://example.com/account/login will alias for http://example.com/index.php/account/login, which is then processed by my front controller. So far so good.

However, if someone explicitly navigates to

http://example.com/index.php/account/login,

I would like it to first redirect to:

http://example.com/account/login,

and then alias for

http://example.com/index.php/account/login.

That way, the user will see http://example.com/account/login in their navigation bar.

How can I handle both of these rules simultaneously in .htaccess, and most importantly, without hardcoding the host and domain (http://example.com) in .htaccess?

This should also work in a subdirectory, for example:

http://example.com/site1/index.php/account/login -> http://example.com/site1/account/login

With the .htaccess file located in the site1 directory relative to document root, and without having to explicitly put site1 in the .htaccess.

You need a new redirect rule before this rule:

RewriteEngine On

RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(?:/(.*))?$ %{ENV:BASE}$1 [L,R=302,NC,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Try this:

RewriteRule ^index\.php/(.*)$ /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]