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

时间:2022-11-24 08:26:59

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

我无法编辑所有文件并删除.php。我怎么能不在地址栏中显示.php,但仍然可以使用.php结尾包含页面

3 个解决方案

#1


11  

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

以下代码应该在.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]

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

还记得这些规则对你的php包含没有影响,例如:include'myheader.php';

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

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

#2


4  

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.

以下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]

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.

这是用于URL重写。至于include语句,您仍然需要PHP扩展,因为这些是物理文件路径。如果你需要这样的东西,你应该看看符号链接。

#3


0  

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

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

#1


11  

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

以下代码应该在.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]

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

还记得这些规则对你的php包含没有影响,例如:include'myheader.php';

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

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

#2


4  

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.

以下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]

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.

这是用于URL重写。至于include语句,您仍然需要PHP扩展,因为这些是物理文件路径。如果你需要这样的东西,你应该看看符号链接。

#3


0  

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

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