使用htaccess隐藏php文件的扩展名和index.php

使用htaccess隐藏php文件的扩展名和index.php,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,这已经被问了一千次了,但不是我想要的。我曾尝试将不同的解决方案组合在一起,但我的.htaccess似乎并没有达到应有的效果 # Not sure what this does? Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine On # To externally redirect /dir/foo.php to /dir/foo RewriteCond %{THE_REQUEST} ^[A-Z]{

这已经被问了一千次了,但不是我想要的。我曾尝试将不同的解决方案组合在一起,但我的.htaccess似乎并没有达到应有的效果

# Not sure what this does?
Options +FollowSymLinks -MultiViews

# Turn mod_rewrite on
RewriteEngine On

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

# Redirect index in any directory to root of that directory
RewriteCond %{THE_REQUEST} ^[A-Z](3,9)\ /([^/]+/)*index\.[^\ ]*\ HTTP/
RewriteRule ^(([^/]+/)*)index(\.[a-z0-9]+)?$ http://www.example.com/$1? [R=301,L] 
现在,我的页面正确地从
domain.com/page1.php
更改为
domain.com/page1
,但是
domain.com/index.php
出现了一些问题

我正在本地测试这一点,当转到
localhost/project
时,一切正常(打开
index.php
,但您在url中看不到),但当您显式导航到
localhost/project/index.php
时,您将返回到根目录,即
localhost
(哪个母鸡返回
http://localhost/xampp/splash.php当然,这不是我想要的。我想要
localhost/project/index.php
返回到'localhost/project/'

不过,还有一个问题:重写规则如何影响搜索引擎。这些页面(如contact.php、about-us.php等)还会被编入索引吗


额外+1,感谢他/她在回答中详细列出了htaccess中每一行/规则的功能。我仍在学习。htaccess,因此每一个细节对我都很重要和相关!

这段代码中的许多注释似乎都是我的:)

无论如何,您可以使用以下代码解决问题:

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /project/

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+project/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1%2 [R=302,L,NE]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/project/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
更新:用于
DocumentRoot

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1%2 [R=302,L,NE]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

谢谢你的快速回答,但这根本不起作用
contact.php
正确地重写为
contact
,但随后我得到一个404错误
Index.php
仍然引用localhost本身,而不是它的基本文件夹。不,它在
http://localhost/project/.htaccess
,所有其他.php文件也在
http://localhost/project
index.php
仍然重定向到
localhost/
,其他链接也可以!也许这和XAMPP有关?还是一样的行为。php重定向到localhost。