PHP URL 没有 .php 扩展名的格式?

PHP URL Format without .php extension?

我只需要获取一个字符串 URL 而无需显示 PHP 文件名。我有文件 contact.php 页面,我正在尝试将 URL 作为 例如

localhost/contact.php

这就是我现在得到的我需要访问这个页面

localhost/contact

对于每个页面,我应该只能通过他们的名字进行导航。

localhost/contact
localhost/help
localhost/support

如果您使用的是 apache,则需要使用 .htaccess 文件。如果您使用的是 IIS,那么您可以在 IIS 中进行配置。

看看this

您的 .htaccess 文件需要如下所示:

IndexIgnore * # prevent directory listing

Order deny,allow
Allow from *

# ------------------------------------------
# Rewrite so that php extentions are not shown
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ .php

由于文件限制,您不能简单地在 windows 中创建一个 .htaccess 文件,因此请打开一个文本编辑器(记事本 ++ 或您的程序 IDE)并创建一个新文件称为 .htaccess。文件名前面的.很重要。

编辑

没有重复的文件名。 IE contact.phpcontact.js 作为 htaccess 将不知道要提供哪个,哪个(取决于您的 apache)将 return 一个错误页面或者它将提供一个只有他们。

正如@AedixRhinedale 在下面的评论中指出的那样:

来自 Apache 的注释:如果您有权访问 httpd 主服务器配置文件,则应完全避免使用 .htaccess 文件。使用 .htaccess 文件会降低 Apache http 服务器的速度。可以包含在 .htaccess 文件中的任何指令最好在目录块中设置,因为它具有相同的效果和更好的性能

就这么简单..

1) 在目录中创建 .htaccess 文件(windows 对此有限制,如上所述)。

2) 只需插入这个..

# Turn mod_rewrite on
RewriteEngine On

##(Optional) Redirects http to https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#### hiding .php extensions below:

# (optional) Ignore logic.php .. any page that is needed as mvc with .php exstensions 
RewriteCond %{THE_REQUEST} ^/logicpath/logic.php [NC]
RewriteCond %{THE_REQUEST} ^/logicpath/funcs.php [NC]
# (optional end ^)

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

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]


# (optional) Does the same for html paths
## To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo.html to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.php [L]
# (optional end ^)

如前所述,httpd 主服务器配置文件要好得多。我不太确定如何解决主要配置文件,但我知道从小经验来看它并不完全相同。

如果其他人知道学习如何修改配置文件的好资源,请提及!