htaccess重写规则隐藏实际文件

时间:2014-03-23 11:43:39

标签: .htaccess mod-rewrite rewrite

这是我的文件夹结构

htdocs
-> testing
   ->index.php
   ->.htaccess

在index.php里面我有这段代码

<?php
    if(isset($_SERVER['PATH_INFO'])) {
        echo $_SERVER['PATH_INFO'];
    }
?>

在.htaccess里面我有这个代码

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]

以下是我的问题:

在上面的htaccess重写规则中,它将我的地址localhost / testing / HelloWorld链接到localhost / testing / index.php / HelloWorld

有可能做这样的事吗?当用户输入 localhost / testing / index.php / HelloWorld 时,我希望浏览器隐藏单词index.php并仅显示类似 localhost / testing / HelloWorld <的内容/ p>

我试图通过在.htacces文件中添加另一条规则将链接 localhost / testing / index.php / HelloWorld 重定向到 localhost / testing / HelloWorld ,但我得到了“该网页有重定向循环“错误消息。

添加新规则后,.htacces文件看起来像这样

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]

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

有什么方法可以让我的链接像这样?非常感谢,非常感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您获得重定向循环,因为重写的url与重定向规则匹配。您需要使用技巧使外部重定向仅发生在具有index.php的外部请求上,而不是发生在将不同请求映射到该文件的内部重写上。您可以使用%{THE_REQUEST}执行此操作,这只会是外部请求。如果您重写映射到的文件,它将不会更新。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /testing/index\.php/
RewriteRule ^index.php/(.+)$ /testing/$1 [R]