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

查看:809
本文介绍了阿帕奇.htacces重写规则删除的.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

推荐答案

随着code应在.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影响包括,例如:包括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.

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

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