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

发布于 2024-11-08 11:38:40 字数 363 浏览 0 评论 0原文

标题解释了基础知识,现在,问题来了:

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

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

而且我无法编辑所有文件并删除 .php。如何在地址栏中不显示.php,但仍然能够使用.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';?>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剑心龙吟 2024-11-15 11:38:40

以下代码应该适用于您在 .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 包含 NO 没有影响,例如: include ' myheader.php';

这是因为这些包含是由 php 本身处理的,不经过 Apache。

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]

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

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

可爱咩 2024-11-15 11:38:40

以下 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 扩展,因为这些是物理文件路径。如果您需要一些奇特的东西,您应该查看符号链接。

The following RewriteRules will assume the PHP extension for any file that is not otherwise matched. It should go at the bottom of your RewriteRules in your .htaccess file.

# 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]

This is for URL Rewriting. As far as include statements, you still need the PHP extension as these are physical file paths. If you need to so something fancy there, you should look into symbolic links.

梦断已成空 2024-11-15 11:38:40

您没有采取正确的途径来解决您试图解决的问题。

如果我理解得很好,你想要做的是有一个像这样的网址:

http://example.com/my/url

而不是(例如):

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

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

You are not taking the correct path for fixing the problem you trying to solve.

If I understood well, what you want to do is having an url like :

http://example.com/my/url

instead of (for example):

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

If this is the case, what you should look for is rewrite url

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文