看起来index.php 文件在您的文档根目录中不是(我假设它是www),因此,我认为您没有办法可以从您的 .htaccess 文件中执行此操作。为了访问文档根目录之外的内容,您需要在服务器配置或虚拟主机配置中设置别名:
# Somewhere in vhost/server config
Alias /index.php /var/www/path/to/index.php
# We need to make sure this path is allowed to be served by apache, otherwise
# you will always get "403 Forbidden" if you try to access "/index.php"
<Directory "/var/www/path/to">
Options None
Order allow,deny
Allow from all
</Directory>
现在您应该可以访问/var/www/path/to/index.php。请注意,/var/www/path/to 目录中的其他文件是安全的,只要您不创建指向它们的Alias(或AliasMatch 或ScriptAlias)。现在您可以通过 /index.php URI 访问 index.php,您可以在文档根目录 (www) 的 .htaccess 文件中设置一些 mod_rewrite 规则,以将内容指向 index.php:
# Turn on the rewrite engine
RewriteEngine On
# Only apply the rule to URI's that don't map to an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all requests ending with ".php" to "/index.php"
RewriteRule ^(.*)\.php$ /index.php [L]
这样当你请求 http://site/page1.php 时,浏览器的地址栏没有改变,但服务器实际上服务于/index.php,别名为/var/www/path/to/index.php .
如果需要,您可以将正则表达式 ^(.*)\.php$ 调整为更合适的值。这仅匹配以.php 结尾的任何内容,包括/blah/bleh/foo/bar/somethingsomething.php。如果要限制目录深度,可以将正则表达式调整为^([^/]+)\.php$等。