.htaccess将所有的扩展名redirect到php

我最近更新了我的.htaccess文件来隐藏URL的.php扩展名 。 它工作的很好,当我试图达到domain.com/index ,它显示的PHP文件。

当我键入domain.com/index.php时,它会将我redirectdomain.com/index url并显示.php文件。

我想将所有其他扩展名redirect到没有扩展名的url,出于安全原因显示.php文件。 我的问题是,我不能configuration我的.htaccess来redirect像.html .asp这样的众所周知的扩展 aspx.shtml等转换为非扩展名的url,并获取显示为内容的.php文件。

我的.htaccess文件如下所示:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / # php extension to no extension url RewriteCond %{THE_REQUEST} ^[AZ]+\s.+\.php\sHTTP/.+ RewriteRule ^(.+)\.php $1 [R=301,L] # no extension to the .php file RewriteCond %{REQUEST_FILENAME}.php -f RewriteCond %{REQUEST_URI} !/$ RewriteRule (.*) $1\.php [L] # this is for the index RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> 

试试这些你的.htaccess文件,请检查重写引擎是否在服务器上,并有mod_rewrite.c模块是在主要的Apacheconfiguration文件是httpd.conf。

1. RewriteRule ^ about $ about.php [L] 2.RewriteEngine on RewriteCond%{REQUEST_FILENAME}!-d RewriteCond%{REQUEST_FILENAME} .php -f RewriteRule ^(。*)$ $ 1.php

希望这些工作正常。

或者,否则这是所有描述的另一种select:

Apache重写规则选项+ FollowSymLinks RewriteEngine在RewriteBase /

将尾部斜杠添加到url

RewriteCond%{REQUEST_FILENAME}!-f RewriteCond%{REQUEST_URI}!(。[a-zA-Z0-9] {1,5} | / |#(。 ))$ RewriteRule ^(。 )$ $ 1 / [R = 301,L]

从url中删除.php-extension

RewriteCond%{REQUEST_FILENAME}!-d RewriteCond%{REQUEST_FILENAME} .php -f RewriteRule ^([^。] +)/ $ $ 1.php

Apache重写规则结束

我想你想知道的事情:

  1. 这是针对不使用前端控制器的网站,所有请求都通过index.php强制执行。 如果你需要的话,你必须用自己的replace第三块。

  2. 该示例仅以.php显示。 如果你想用.html作为例子,你必须复制/粘贴第三个块,并用“.html”replace第(1,),第3和第4行中的“.php”(除非有一个干净利落的方法) ?)

  3. “example.com/something/”的内容可以从“example.com/something.php”或“example.com/something/index.php”中获取。

  4. 要链接到其他页面,只需href =“/ some / thing /”或href =“// example.com/some/thing/”。

  5. 用.css和.js你想要href =“/ style.css”或者href =“/ folder1 / style.css”。 否则,如果你从“example.com/folder2/file.php”href =“style.css”,它会看“example.com/folder2/style.css”。

注意:在所有example.com的前面都有http://。

如果没有其他“。” 在您的文件扩展名除外,你是否尝试用任何stringreplace“.php”?

 # php extension to no extension url RewriteCond %{THE_REQUEST} ^[AZ]+\s.+\.\s+\sHTTP/.+ RewriteRule ^(.+)\.php $1 [R=301,L] 

尝试下面的代码,并更改扩展名,无论你想隐藏:

1.对于html文件

 RewriteCond %{THE_REQUEST} ^[AZ]{3,}\s([^.]+)\.html [NC] RewriteRule ^ %1 [R,L,NC] RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^ %{REQUEST_URI}.html [L] 

2.对于php文件

 RewriteCond %{THE_REQUEST} ^[AZ]{3,}\s([^.]+)\.php [NC] RewriteRule ^ %1 [R,L,NC] RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^ %{REQUEST_URI}.php [L]