mod_rewrite从特定文件中删除.php并阻止访问原始文件

时间:2017-06-01 13:34:29

标签: apache .htaccess mod-rewrite

我想改变:

www.foo.com/bar.php

要:

www.foo.com/bar

之后,阻止直接访问/bar.php,但允许/ bar:

www.foo.com/bar.php    404 Not Found

我想访问bar.php给出404错误 基本上,我希望它看起来像“bar”是一个目录。

这甚至可能吗?

我目前的代码是:

RewriteEngine On
RewriteRule ^bar/?$ bar.php [NC]

1 个答案:

答案 0 :(得分:1)

您可以使用:

RewriteEngine on
#prevent direct access to /bar.php
#return a 404 error if /bar.php is direct accessed
RewriteCond %{THE_REQUEST} /bar\.php [NC]
RewriteRule ^ - [R=404,L]
#rewrite /bar to /bar.php
#this will internally forward http://example.com/bar to http://example.com/bar.php
RewriteRule ^bar$ /bar.php [L]