1

I found this question:

and it was useful but i'm not able to write code for my system.

This is my URL structure:

www.example.com/typeOfPost/category/ID/permalink

(For example: www.example.com/news/sport/8734/here-goes-article-title)

My cache structure is this:

ROOT/cache/responsive/ID/typeOfPost/category/permalink

where permalink is a file without extension that contains the html code of the page.

Unfortunately for cache file I can't use the same URL structure so I don't know how to check if cache file exists and if so load it, otherwise load data from database.

At the moment I'm reading checking for cache file via PHP and if it exists I load it otherwise I load data from database.

For server performance I'd like to to bypass PHP and check for cache file via Apache, I think this is the right approach... isn't it? If so, how can I write the .htaccess file?

This is the code i wrote but it doesn't work:

RewriteCond %{DOCUMENT_ROOT}/cache/responsive/([0-9]+)/([^/]+)/([^/]+)/([^/]+) -f
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)$ /cache/responsive/$3/$1/$2/$4 [L]
MrWhite
  • 43,179
  • 8
  • 60
  • 84
Francesco
  • 11
  • 1

1 Answers1

0
RewriteCond %{DOCUMENT_ROOT}/cache/responsive/([0-9]+)/([^/]+)/([^/]+)/([^/]+) -f
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)$ /cache/responsive/$3/$1/$2/$4 [L]

The preceding condition (RewriteCond directive) should be checking for the existence of the same file that you are wanting to rewrite the request to in the RewriteRule substitution string. As in the question/example you linked to, which is checking for the existence of %{DOCUMENT_ROOT}/cache/$0.html before ultimately rewriting the request to cache/$0.html - as you can see, they are the same. In your code, they clearly are not. Otherwise, everything else about your rule would seem to be OK.

So, your rule should be written like this instead:

RewriteCond %{DOCUMENT_ROOT}/cache/responsive/$3/$1/$2/$4 -f
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([^/]+)$ /cache/responsive/$3/$1/$2/$4 [L]

The RewriteRule directive first matches against the requested URL. If it matches then the preceding condition (RewriteCond directive) checks that the "cache" file exists. If the preceding condition is successful then the RewriteRule directive performs the substitution as stated.

However, the slash prefix on the substitution is unnecessary and should be omitted, so that it rewrites to a filesystem path, as a opposed to a URL-path (which triggers mod_rewrite to remap the request to the filesystem). Although the net result is the same in this instance.

You also don't need to capture the first two path segments separately, as these remain as a pair in the cache file path.

For example, it could be written like this instead:

RewriteCond %{DOCUMENT_ROOT}/cache/responsive/$2/$1/$3 -f
RewriteRule ^([^/]+/[^/]+)/(\d+)/([^/]+)$ cache/responsive/$2/$1/$3 [L]

\d is a shorthand character class that represents digits and is the same as [0-9]

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Thanks this works perfectly! Now i just noticed that this caching approach opens another question: at the moment cache is served via php so i can check if admin is logged (via php session variable) and in that case serve the db version otherwhise serve the cached file. With htaccess approach cached file is always served both if i'm logged or not. i found this: https://stackoverflow.com/questions/342509/get-php-session-vars-in-htaccess and the workaround 2 could be a solution but it is not reccomended... can i ask you what you think about that? – Francesco Sep 28 '22 at 09:40