PHP/.htaccess:从 url 中删除 php 扩展名

2023-01-18php开发问题
5

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

问题描述

我使用 .htaccess 代码删除了所有网页的 .php 扩展名.这是我使用的代码:

I was using .htaccess code to remove .php extension for all my web pages. Here's the code I use:

RewriteEngine On
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-s]+)/$ /$1.php

它似乎不起作用.我想我错过了一些东西.当我输入 www.mysite.com/about/以获取 www.mysite.com/about.php 时,它返回错误 404(未找到页面).有人可以请说明一下.

It doesn't seem to work. I think I'm missing something. When I type www.mysite.com/about/ to get www.mysite.com/about.php it returns error 404 (page not found). Can someone please shed some light.

谢谢,保罗 G.

推荐答案

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# If folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# and file exist
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# uncomment the below rule if you want the "/" to be required 
# otherwise leave as is
# RewriteRule ^([^/]+)/$ $1.php [L]
# internally show the content of filename.php
RewriteRule ^([^/]+)/?$ $1.php [L]

以上规则将:

  1. 如果文件夹存在则不会重定向
  2. 如果文件不存在则不会重定向
  3. 将重定向 / 之前的内容,如果存在作为文件名
  1. will not redirect if a folder exist
  2. will not redirect if the file does not exist
  3. will redirect what comes before the / if one is present as the file name

所以它适用于所有这些示例:

So it will work for all these examples:

http://domain.com/about/
http://domain.com/about
http://domain.com/contact/
http://domain.com/contact

如果你愿意,你可以删除 ?,就像注释规则一样,让它只接受以 / 结尾的 URL.

If you want you can remove the ?, like the commented rule, to make it accept only URL's that end with a /.

http://domain.com/about/
http://domain.com/contact/

现在这些是上述工作的重要步骤:

Now these are important step for the above to work:

  1. 它必须进入您的根文件夹中的 .htaccess,例如 /home/youraccount/public_html/.htaccess
  2. 重写规则前的选项很重要,特别是-MultiViews
  3. 该文件必须与 .htaccess 位于同一位置,例如在您的情况下是 about.php 文件
  4. PHP 必须运行正常.
  1. It must go into the .htaccess on your root folder for example /home/youraccount/public_html/.htaccess
  2. The Options before the rewrite rule are very important specially -MultiViews
  3. The file must exist on the same place the .htaccess is for example in your case the about.php file
  4. The PHP must be working obviously.

这篇关于PHP/.htaccess:从 url 中删除 php 扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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