Apache .htaccess 重写规则以删除 .php 文件扩展名 [英] Apache .htacces Rewrite Rule to Remove .php File Extensions

查看:38
本文介绍了Apache .htaccess 重写规则以删除 .php 文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了基础,现在问题来了:

The title explains the basics, now, here comes the problem:

我有很多看起来像这样的文件:

I have many files that look like this:

<?php include 'mynavbar.php';   
include 'myheader.php';    
include 'myfirstline.php'; 
include 'mybuttons.php';   
include 'myadvert.php'; 
include 'myimg.php';?>

而且我无法编辑所有文件并删除 .php.如何在地址栏中不显示.php但仍然能够通过使用.php结尾来包含页面

And I can't edit all the files and remove the .php. How can I not show the .php in the address bar but still be able to include the pages by using the .php ending

推荐答案

以下代码应该适用于 .htaccess 文件中的隐藏 .php 扩展名:

Following code should work for you in .htaccess file to hide .php extension:

Options +FollowSymlinks -MultiViews
RewriteEngine on

# to make `/path/index.php` to /path/
RewriteCond %{THE_REQUEST} ^GET\s(.*/)index\.php [NC]
RewriteRule . %1 [NE,R=301,L]

RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

还请记住,这些规则将没有影响您的 php 包含,例如:include 'myheader.php';

Also remember that these rules will have NO impact in your php includes eg: include 'myheader.php';

这是因为那些包含是由 php 本身处理的,而不是通过 Apache.

It is because those includes are processed by php itself and don't go through Apache.

这篇关于Apache .htaccess 重写规则以删除 .php 文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆