d.raev
d.raev

Reputation: 9546

RewriteRule for the root path

I made a apache2 rewrite so that the root request "www.example.com" loads the content from the cache, but all other requests with params go normally through index.php "www.example.com/?action=1".

(please don't comment on the caching approach as it is a bit more complex in the real case)

*** 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]

The problem is it worked nice on my machine .. but does not work on the server. bot environments are similar:
local - vagrant box with Ubuntu 13.04
server - Ubuntu 14.04 The configurations are standard and similar as they run only this project.

I guess there is some change or specific settings that breaks it but can not figure it out.

Upvotes: 0

Views: 55

Answers (2)

d.raev
d.raev

Reputation: 9546

I ended up fixing it like this:

    # 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]

Not sure what looping is happening but the END in the last rule fixes it. I will be happy to accept better structured or explained solution.

Upvotes: 0

poncha
poncha

Reputation: 7866

Query string is not part of request uri, which you're rewriting. So, if you want to ensure rewrite happens only on empty query string, You need to add a condition:

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

And then your regular rules.

Upvotes: 1

Related Questions