【问题标题】:htaccess rewrite rule to hide actual filehtaccess 重写规则以隐藏实际文件
【发布时间】:2014-03-23 11:43:38
【问题描述】:

这是我的文件夹结构

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

我尝试通过在 .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]

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

【问题讨论】:

    标签: .htaccess mod-rewrite rewrite


    【解决方案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]
    

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 2011-09-23
      • 2018-09-18
      • 2016-02-28
      • 2015-09-12
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多