繁体   English   中英

Apache .htacces重写规则以删除.php文件扩展名

[英]Apache .htacces Rewrite Rule to Remove .php File Extensions

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

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

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

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

以下代码应在.htaccess文件中为您工作以隐藏.php扩展名:

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';

这是因为这些包含项是由php本身处理的,不会通过Apache进行处理。

以下RewriteRules将为任何其他不匹配的文件采用 PHP扩展名。 它应该放在.htaccess文件中RewriteRules的底部。

# Force PHP extension if not a directory
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^(.*)$ - [L]

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

这是用于URL重写的。 至于include语句,您仍然需要PHP扩展,因为它们是物理文件路径。 如果您需要在那里做些花哨的事情,则应查看符号链接。

您没有采用正确的方法来解决要解决的问题。

如果我理解得很好,那么您想要做的就是输入一个网址,例如:

http://example.com/my/url

而不是(例如):

http://example.com/my/url.php

如果是这种情况,您应该寻找的是重写网址

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM