mod_rewrite 在 apache 2.4 上无法正常工作

Posted

技术标签:

【中文标题】mod_rewrite 在 apache 2.4 上无法正常工作【英文标题】:mod_rewrite works incorrectly on apache 2.4 【发布时间】:2015-01-14 06:23:20 【问题描述】:

我之前用过其他主机,一切正常,但现在我已经将其更改为新主机。在新主机上安装了 apache 2.4。

我有以下规则:

<IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteBase /
        RewriteCond %HTTP_HOST ^www\.(.*) [NC]
        RewriteRule ^/?(.*) http://%1/$1 [L,R=permanent]

        RewriteRule manage/ajax/(.*?)$ manage/ajax/index.php [QSA,L]
        RewriteRule manage/json/(.*?)$ manage/json/index.php [QSA,L]

        RewriteCond %REQUEST_URI !\.(.+)$
        RewriteRule ckfinder/(.*?)$ ckfinder/index.html [QSA,L]

        RewriteCond %REQUEST_URI !\.(.+)$
        RewriteRule manage/(.*?)/$ manage/index.php [QSA,L]

        RewriteCond %REQUEST_URI !\.(.+)$
        RewriteRule ajax/(.+?) ajax/index.php [QSA,L]

        RewriteCond %REQUEST_URI !\.(.+)$
        RewriteRule api/(.*?) /api/index.php [QSA,L]

        RewriteCond %REQUEST_URI !\.(.+)$
        RewriteRule (.*) index.php
</IfModule>

当我尝试访问例如 /manage/ 时,它会显示索引页面,但如果我将一些额外的数据添加到路径中,它将显示我需要的内容。例如:/manage/some_data - 可以正常工作。但我想通过 /manage/ 访问正确的目录。

【问题讨论】:

只需在 .htaccess 顶部使用 DirectoryIndex index.php 谢谢,这解决了我的问题。 不客气,很高兴它有帮助。 【参考方案1】:

更改此规则:

RewriteCond %REQUEST_URI !\.(.+)$
RwriteRule manage/(.*?)/$ manage/index.php [QSA,L]

RewriteCond %REQUEST_URI !\.(.+)$
RwriteRule manage(/.*)?$ manage/index.php [QSA,L]

【讨论】:

同样的结果,没有任何改变。 绝对确定您输入正确?正则表达式应该可以工作。见这里:regex101.com/r/jN1jA8/2 我不认为问题出在正则表达式中,因为我的规则在旧服务器上运行良好。我认为逻辑上存在问题,也许在新的 apache 中使用 mod_rewrite 发生了一些变化? 好吧,您的旧正则表达式看起来不正确,它只会匹配您所看到的,即manage/some_datamanage//(.*?) 部分会阻止manage/一场比赛。 好吧,也许我的正则表达式也错了,但你的正则表达式并没有解决我的问题。此外,如您所见,还有 api、ajax 和其他。我对所有这些查询都有同样的问题。我觉得主逻辑有问题。

以上是关于mod_rewrite 在 apache 2.4 上无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

WAMP 2.4 上的 Mod_rewrite

在Apache中使用mod_rewrite模块重写URL

apache mod_rewrite 模块未安装

Ubuntu系统启用Apache Mod_rewrite模块

Apache - 添加虚拟目录后无法再使用 mod_rewrite

Apache 的 mod_rewrite VS。 PHP路由?