【问题标题】:Mod Rewrite to rewrite *.html files (CodeIgniter)Mod Rewrite 重写 *.html 文件 (CodeIgniter)
【发布时间】:2010-06-29 03:25:19
【问题描述】:

我确信这可能真的很简单,但是我在这方面的知识确实不好,而且我似乎无法正确理解。

我的文件结构如下:

/cms (renamed from system)
cms.php (renamed from index.php, added to DirectoryIndex in .htaccess)
.htaccess
index.html
page1.html
/css
/js

我也有来自 CI wiki 的 .htaccess。我认为需要调整的部分是:

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to cms.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ cms.php?/$1 [L]

我希望大多数已经存在的文件都可以访问,例如和 .js、.css、.png、.swf 等,但任何 .htm 或 .html 都将由 cms.php 处理。

例如,如果用户请求http://localhost/index.html,则将index.html 传递给cms.php,或者如果请求http://localhost/dir/page.html,则传递dir/page.html。

【问题讨论】:

  • 出于好奇,您所拥有的有什么不妥之处?
  • 它工作得很好,但我试图捕获 html 文件以在输出之前处理它们。例如,用户请求 index.html,“index.html”被传递给加载文件的 codeigniter 应用程序,进行任何更改,然后回显内容。需要它以这种方式工作以保持简单 - 设计人员只需在其代码中转储 3 个文件(/cms、cms.php 和 .htaccess)即可对其进行 cms 化。
  • 啊,我明白了。 (我知道这个问题的原始答案存在问题,但在我确定你想要什么之前不想发布答案,看起来你已经弄清楚了,很高兴听到它的工作!)跨度>

标签: .htaccess mod-rewrite codeigniter


【解决方案1】:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(*\.js|*\.png|*\.css|*\.swf|folder1|folder2)
RewriteRule ^(.*)$ index.php/$1 
RewriteRule ^.\.htm cms\.php
RewriteRule ^.\.html cms\.php
</IfModule>

您可以添加不希望重定向到RewriteCond 行的其他扩展和/或文件夹。

【讨论】:

  • 啊,好的。有没有办法扭转它,所以我指定应该重写哪些文件(.htm,.html)?
  • 是的。您可以在文件中添加额外的RewriteRule 行。我用 2 个附加重写规则示例编辑了答案,这些规则将 .htm.html 文件重定向到 cms.php
  • 嗯,无法正常工作。但是,我想我已经对其进行了排序 - 找到了一个关于它的教程,并提出了 RewriteRule ^(.*\.html)$ cms.php?/$1 [NC] 似乎可以完成这项工作。还是谢谢!
【解决方案2】:

找到了一个教程,并想出了以下内容:

RewriteRule ^(.*\.html)$ cms.php?/$1 [NC]

所以任何以 .html 结尾的请求都会传递给 cms.php,从而生成一个类似 cms.php/index.html 的 URL,cms.php 可以处理和输出该 URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-26
    • 2015-09-22
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多