【问题标题】:htaccess: remove subdirectory form URL while serving up original contenthtaccess:在提供原始内容时从 URL 中删除子目录
【发布时间】:2012-02-11 17:41:55
【问题描述】:

我有这样的 URL 结构

http://domain.com/page/thing1
http://domain.com/page/thing2
http://domain.com/page/thing3

我想:

  • 从 URL 中删除 /page/
  • 提供来自 /page/thing1 的内容

我正在使用 ExpressionEngine,所以这是我从 URL 中删除 index.php 的方法

<IfModule mod_rewrite.c>
        RewriteEngine On

        # Removes index.php
        RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

【问题讨论】:

  • 您不一定必须通过 ModRewrite 执行此操作,具体取决于您在 EE 中访问此数据的方式。这些 URL 是通过 Pages 模块创建的吗?还是page 是模板组而thing1 是模板?或者是thing1thing2 等,所有条目的 URL 标题?
  • @DerekHogue 现在我已经设置好了,其中 page 是一个模板组,thing1 和 thing 2 是条目。
  • 在这种情况下,see rjb's answer - Pages 模块是这里的方法。通过第一段通过 URL 标题访问单个条目不是一个好方法。

标签: apache .htaccess mod-rewrite redirect expressionengine


【解决方案1】:

这些新网址有什么好处?

简单地从您的 URI 中删除 /page URL 段(即模板组)会产生大量额外的开销和复杂性,如下面的ExpressionEngine URL Schematic 所示:

原生 ExpressionEngine Pages Module 或第三方 Structure Module 是最简单的方法,不需要在您的 .htaccess 文件中添加任何 RewriteRules 规则。

但是,这将要求您为每个条目创建一个页面 URL,这可能不太理想。

【讨论】:

    【解决方案2】:

    如果你把你所有的url都改成http://domain.com/thing1http://domain.com/thing2等,你就不能在重写index.php文件的时候把page/加回来吗? p>

    RewriteRule ^(.*)$ /index.php/page/$1 [L]
    

    【讨论】:

    • 这将假定 所有请求 将通过 /pages 模板组进行路由,这会破坏许多 URI — 包括许多核心 ExpressionEngine URL。至少,您必须将RewriteRuleRewriteCond 结合起来。
    【解决方案3】:

    IfModule 块中的现有重写规则下方附加这些行:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(?!page/)(.*)$ /page/$1 [L,NC]
    

    【讨论】:

      猜你喜欢
      • 2012-06-25
      • 2021-06-24
      • 2020-01-09
      • 2019-10-03
      • 2013-08-18
      • 2011-09-24
      • 2012-03-19
      • 1970-01-01
      • 2013-05-18
      相关资源
      最近更新 更多