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

2023-09-23php开发问题
2

本文介绍了没有 .php 扩展名的 PHP URL 格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

all I need is to get a string as URL without showing the PHP filename. I have file contact.php page I am trying to get URL as e.g.

本地主机/联系人.php

localhost/contact.php

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

this is what now I am getting I need to access this page just with

本地主机/联系人

对于每个页面,我应该只能通过它们的名称导航.

For every page I should able to navigate by their name only.

本地主机/联系人
本地主机/帮助
本地主机/支持

localhost/contact
localhost/help
localhost/support

推荐答案

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

You need to use an .htaccess file if you are using apache. If you are using IIS then you can configure it in IIS.

看看这个.

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

Your .htaccess file would need to look something like this:

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 ^(.*)$ $1.php

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

You cannot simply create a .htaccess file in windows because of file constraints, so open a text editor (Notepad ++ or your programming IDE) and create a new file called .htaccess. The . is important in front of the file name.

编辑

不要有重复的文件名.IE contact.phpcontact.js 作为 htaccess 将不知道要提供哪个服务,哪个(取决于您的 apache)将返回一个错误页面,否则它只会提供其中一个.

Do not have duplicate file names. IE contact.php and contact.js as the htaccess will not know which one to serve, which (depending on your apache) will either return an error page or it will serve one of them only.

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

As @AedixRhinedale pointed out in the comments below:

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

Just a note from Apache: You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance

这篇关于没有 .php 扩展名的 PHP URL 格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17