【问题标题】:mod_rewrite to alias one file suffix type to anothermod_rewrite 将一个文件后缀类型别名为另一个
【发布时间】:2010-09-05 17:57:31
【问题描述】:

我希望我能解释清楚,但如果不让我知道,我会尽力澄清。

我目前正在使用 ColdFusion 开发一个站点,并设置了一个 mod_rewrite 规则以使其看起来像该站点使用的是 PHP。对 index.php 的任何请求都由 index.cfm 处理(规则将 *.php 映射到 *.cfm)。

这很好用 - 到目前为止,一切都很好。问题是如果直接请求 index.cfm(或任何 ColdFusion 页面),我想返回 404 状态代码。

如果我尝试使用 mod_rewrite 阻止对 *.cfm 文件的访问,它还会返回 404 请求 *.php。

我想我可能不得不更改我的 Apache 配置而不是使用 .htaccess

【问题讨论】:

    标签: apache mod-rewrite seo


    【解决方案1】:

    您可以使用S 标志跳过404 规则,如下所示:

    RewriteEngine on
    
    # Do not separate these two rules so long as the first has S=1
    RewriteRule (.*)\.php$ $1.cfm [S=1]
    RewriteRule \.cfm$ - [R=404]
    

    如果您还使用Alias 选项,那么您还应该添加PT 标志。详情请见mod_rewrite documentation

    【讨论】:

      【解决方案2】:

      您必须使用两组不同的重写规则,一组用于 .php,另一组用于 .chm,并使它们与 RewriteCond %{REQUEST_FILENAME} 互斥。并按照 jj33 的建议使用标志 [L]。

      您可以将规则保存在 .htaccess 中。

      【讨论】:

        【解决方案3】:

        发布您已有的规则作为起点,这样人们就不必重新创建它来帮助您。

        我建议首先尝试将 .php 映射到 .cfm 文件的规则测试 [L]。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-01-26
          • 1970-01-01
          • 1970-01-01
          • 2022-01-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-30
          相关资源
          最近更新 更多