如何从我的 PHP 页面中删除 .php 扩展名?

How can I remove the .php extension from my PHP page?

我正在尝试从我的 PHP 页面中删除扩展程序。

举个例子,www.mywebsite.com/verification.php

我希望人们能够通过 www.mywebsite.com/verification 进行验证。

我的代码:

<?php session_start(); ?>
!DOCTYPE html
html lang="en"
head>

meta http-equiv="Content-Type" content="text/html; charset=UTF-8"

根据this website

像这样更改您的 .htaccess 文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .php [NC,L]

你可以做类似的事情,使用 .html 扩展名,除了你将最后一行更改为:

RewriteRule ^([^\.]+)$ .html [NC,L]

如果您没有 .htaccess 文件,请执行@Cagy79 所说的操作:

You could make a directory /verification/ and put your script in an index.php.

In this way a user can go to www.yoursite/verification/ without the need of adding the index.php part

此外,您还可以使用 PHP manual security section

中提到的这两个选项
  1. 您可以在 php.ini 文件中将 expose_php 设置为 off。那么在header.

  2. 中将不会发送您服务器上关于运行ning PHP的任何信息
  3. 您可以使用 .htaccess 文件选项来隐藏文件名。提到的可用选项是:

使 PHP 代码看起来像其他代码类型

AddType application/x-httpd-php .asp .py .pl

使 PHP 代码看起来像未知类型

AddType application/x-httpd-php .bop .foo .133t

使所有 PHP 代码看起来像 HTML

AddType application/x-httpd-php .htm .html

不推荐最后一种方法,因为所有 HTML 文件都会 运行 通过 PHP 解释器,即使这不是必需的。

您可以创建一个目录 /verification/ 并将您的脚本放在 index.php.

通过这种方式,用户可以转到 www.mywebsite.com/verification/ 而无需添加 index.php 部分。

(此外,当您无法编辑 .htaccess 文件或不在 Apache 上时也可以这样做)