1

Suppose in root directory I have one file (called index.php) and one folder (called caches). I want that if the file exist in caches folder serve that file (caches/xxx.html) otherwise request send to index.php.

For example I will send request to server: https://example.com/how-to-do and Apache search first in cache/. If how-to-do.html exists then send (rewrite Apache) how-to-do.html otherwise send request to index.php.

This my .htaccess:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
MrWhite
  • 43,179
  • 8
  • 60
  • 84
Mahdi gh hastam
  • 135
  • 1
  • 7
  • Are you only dealing with requests for the document root, or would `/foo/bar/how-to-do` also be a valid request? – MrWhite Mar 15 '22 at 16:31
  • thanks,for fix problem (nested folder) i change your code from `^[^/.]+$ cache/$0.html ` to `(.*) cache/$0.html` in your opinion is this problem ? – Mahdi gh hastam Mar 16 '22 at 10:21
  • 1
    If you are using the `$0` backreference then you don't need to surround the pattern in parentheses. (By surrounding the pattern in parentheses you are creating a `$1` backreference.) However, the regex `.*` is rather too general and inefficient here. Regex should be as specific as possible. The regex `.*` also matches the target URL (ie. `cache/.html`) so results in an additional (unnecessary) filesystem check. If your URLs don't contain dots then use `^[^.]+$` instead to match multiple path depths (simply removing the slash from character class). – MrWhite Mar 16 '22 at 10:57

1 Answers1

1

Immediately before your last rule (ie. before the # Send Requests To Front Controller... comment) you can add something like the following:

# Check if cache file exists and serve from cache if it is
RewriteCond %{DOCUMENT_ROOT}/cache/$0.html -f
RewriteRule ^[^/.]+$ cache/$0.html [L]

This only checks for requests that target the document root, eg. /how-to-do - as in your example. It also assumes that your URL-path does not contain a dot (used to delimit file extensions). It does not target requests with multiple path segments, eg. /foo/bar.

To match multiple path segments then simply remove the slash from the regex character class. ie. ^[^.]+$.

The RewriteRule pattern ^[^/.]+$ matches any non-empty URL-path that does not contain the characters slash and dot. In other words it matches URL-paths that consist of a single path segment, excluding files (that naturally include a dot before the file extension). In .htaccess, the URL-path that is matched by the RewriteRule pattern does not start with a slash.

$0 is a backreference that contains the entire URL-path that is matched by the RewriteRule pattern (ie. ^[^/.]+$).


Reference

The official Apache docs should be your go to reference for this (although the docs are rather concise and somewhat lacking examples in places):

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • It works exactly and I think $0 does the main work Can you tell me the source of your training in this field? I have been studying regex and .htaccess for almost two weeks and I could not solve it! – Mahdi gh hastam Mar 16 '22 at 08:36
  • 1
    @Mahdighhastam I've updated my answer with more explanation and some reference links. The Apache docs are my go-to reference (although not to everyone's taste it would seem). The [mod_rewrite introduction](https://httpd.apache.org/docs/current/rewrite/intro.html) is a good place to start. It is good to question everything and not to copy/paste `.htaccess` without understanding what they do. Best of luck :) – MrWhite Mar 16 '22 at 10:32