RewriteRule用于根路径

时间:2014-09-08 09:34:09

标签: php ubuntu mod-rewrite

我进行了apache2重写,以便根请求"www.example.com"加载缓存中的内容,但所有其他带有params的请求通常会通过index.php "www.example.com/?action=1"

(请不要对缓存方法发表评论,因为它在实际案例中要复杂一点)

*** Vhost Confing file,  NO.htaccess used ***

        RewriteRule ^$ _cache/index.html [NC,L]  #works on local only
        # RewriteRule ^/$ _cache/index.html [NC,L] #
        # RewriteRule ^index.php$ _cache/index.html [NC,L] #breaks normal requests

        RewriteCond %{REQUEST_FILENAME} -s [OR]
        RewriteCond %{REQUEST_FILENAME} -l [OR]
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^.*$ - [NC,L]
        RewriteRule ^.*$ /index.php [NC,L]

问题是它在我的机器上工作得很好..但是在服务器上不起作用。 机器人环境类似:
本地 - Ubuntu 13.04的流浪盒子 服务器 - Ubuntu 14.04 配置是标准的,类似的,因为它们只运行这个项目。

我想有一些改变或特定的设置打破了它但无法弄明白。

2 个答案:

答案 0 :(得分:1)

查询字符串不是您要重写的请求uri的一部分。 因此,如果您想确保仅在空查询字符串上进行重写,则需要添加条件

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^$ _cache/index.html [L]

然后是你的常规规则。

答案 1 :(得分:0)

我最终解决了这个问题:

    # RewriteRule ^$ _cache/%{HTTP_HOST}.html [NC,END]
    RewriteRule ^(|/|index.php)$ _cache/%{HTTP_HOST}.html [NC,L]

    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^.*$ /index.php [NC,END]

不确定发生了什么循环,但最后一条规则中的END修复了它。 我很乐意接受更好的结构化或解释解决方案。

相关问题