【问题标题】:using .htaccess to redirect from friendly url to actual file使用 .htaccess 从友好的 url 重定向到实际文件
【发布时间】:2011-02-26 13:14:55
【问题描述】:

我的 .htaccess 中有以下 RewriteRule 可以从友好的 url 重定向到我的主应用程序文件:

RewriteRule ^\/(.*).html$ home/www/page.php?p=$1 [L]

这应该将指向 html 页面的任何 url 发送到 page.php,并将该 url 作为将由应用程序解析的参数。 这适用于看起来像 http://www.example.com/hello.html

的网址

问题是,当url包含目录路径时出现404错误,例如:http://www.example.com/category/hello.html

错误显示:“文件不存在:/home/www/category”

似乎它首先寻找“类别”路径而不是处理 .htaccess 任何想法如何解决这个问题?

【问题讨论】:

    标签: apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您是否尝试过从重写规则中删除最初的 \/

    我刚刚尝试了以下.htaccess 文件:

    RewriteEngine On
    RewriteRule ^(.*)\.html$ page.php?p=$1 [L]
    

    以此为我的page.php:

    <?php print_r($_GET); ?>
    

    当我转到 /category/hello.html 时,我得到以下信息:

    Array ( [p] => category/hello )
    

    完全符合预期。

    还请注意,您需要在html 之前转义.,因为您可能不希望/category/hellozhtml 工作。

    【讨论】:

      猜你喜欢
      • 2019-08-01
      • 2014-02-24
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多