secondman
secondman

Reputation: 3287

Checking for Directory or Fallback htaccess

I need to check if a directory exists and if not, fall back to the dynamic page, post etc.

This is close but not quite right, I keep getting the Wordpress dynamic post so my condition is wrong:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond wp-content/wp_fast_cache/example.com/%{REQUEST_FILENAME} -d
    RewriteRule ^([^?]*) wp-content/wp_fast_cache/example.com/$1 [L]
    RewriteRule . /index.php [L]
</IfModule>

--EDIT--

The following code now finds the static cache files, but fails to fallback to the default index.php file for items that have been deleted from the cache.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/wp-content/wp_fast_cache/example.com%{REQUEST_URI} -d
    RewriteRule ^([^?]*) wp-content/wp_fast_cache/example.com/$1 [L]
</IfModule>

I've tried the default in the Wordpress htaccess:

RewriteRule . /index.php [L]

But this forces all requests to rewrite to index.php instead of using the cache.

Upvotes: 2

Views: 122

Answers (2)

secondman
secondman

Reputation: 3287

This finally ended up being the solution:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{DOCUMENT_ROOT}/wp-content/wp_fast_cache/example.com%{REQUEST_URI}index.html -f
    RewriteRule ^([^?]*) wp-content/wp_fast_cache/example.com/$1/index.html [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

Very close to what Jon Lin provided, thank you for your help.

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143966

You probably want something more like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # if the request is already "index.php" pass-through and stop rewriting
    RewriteRule ^index\.php$ - [L]

    # if the request exists in the fast cache, rewrite it to that
    RewriteCond %{DOCUMENT_ROOT}/wp-content/wp_fast_cache/example.com%{REQUEST_URI} -d [OR]
    RewriteCond %{DOCUMENT_ROOT}/wp-content/wp_fast_cache/example.com%{REQUEST_URI} -f
    RewriteRule ^ wp-content/wp_fast_cache/example.com%{REQUEST_URI} [L]

    # else, route through wordpress
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

Upvotes: 2

Related Questions