# ============================================================ #
#                                                              #
#   MiMiMi framework & CMS                                     #
#       Copyright 2022 MiMiMi Community                        #
#           > www.mimimi.software                              #
#       Licensed under CC BY 4                                 #
#           > www.creativecommons.org/licenses/by/4.0          #
#                                                              #
#   --------------------------------------------------------   #
#                                                              #
#   This file is a configuration of the Apache web server      #
#   to forward incoming requests to the starting point of      #
#   your website.                                              #
#                                                              #
# ============================================================ #

    # ======================================================== #
    #                                                          #
    #   Override some options for content negotiation:         #
    #                                                          #
    #       -MultiViews = Do not search for matching files     #
    #                     if the directory requested by        #
    #                     the URI does not exist.              #
    #                                                          #
    #       -Indexes    = Do not return a formatted file       #
    #                     listing of the directory that does   #
    #                     not contain the "DirectoryIndex"     #
    #                     file (for example index.html or      #
    #                     index.php).                          #
    #                                                          #
    # ======================================================== #

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

    # ======================================================== #
    #                                                          #
    #   Set up some options for URI redirecting:               #
    #                                                          #
    #       1. Block requests for the unsafe files.            #
    #                                                          #
    #       2. Disable URIs ended with "index.php" and         #
    #          redirect them to the same URI without this      #
    #          ending.                                         #
    #                                                          #
    #       3. Disable URIs with the trailing slash and        #
    #          redirect them to the same URI without           #
    #          the slash.                                      #
    #                                                          #
    #       4. Skip requests for non-script files located      #
    #          in the root MEDIA folder.                       #
    #                                                          #
    #       5. Skip requests for theme files located           #
    #          in any subdirectory (CSS, or FONTS, or IMAGES,  #
    #          or JS) of the THEMES folder.                    #
    #                                                          #
    #       6. Skip requests for the root FAVICON, ROBOTS,     #
    #          or SITEMAP file.                                #
    #                                                          #
    #       7. Send other requests to the starting point of    #
    #          your website.                                   #
    #                                                          #
    #   ----------------------------------------------------   #
    #                                                          #
    #   Used flags:                                            #
    #       -f    = Checks if the request URI is a regular     #
    #               file.                                      #
    #       -     = Tells the rewriting engine to pass the     #
    #               request URI unchanged.                     #
    #       [NC]  = Makes the pattern comparison               #
    #               case-insensitive.                          #
    #       [F]   = Returns a 403 FORBIDDEN response to the    #
    #               client browser.                            #
    #       [R=N] = Forces an external redirect with the       #
    #               "N" HTTP status code.                      #
    #       [QSA] = Appends any query string from the          #
    #               original request URI to any query string   #
    #               created in the rewrite target.             #
    #       [L]   = Breaks the current round of rewrite        #
    #               processing.                                #
    #       [END] = Stops the rewriting process immediately.   #
    #                                                          #
    # ======================================================== #

    <IfModule mod_rewrite.c>
        RewriteEngine  On

        # Step 1
        RewriteCond  %{REQUEST_FILENAME}  -f
        RewriteRule  \.(php\d|phtml?|pl|py|f?cgi|aspx?|c?shtml?|jhtml|jnlp|htms|gscript|hh?|cc?|cxx|cpp|dic|vc4|vcx?proj|lua|vb[es]?|p|pas|asm|f|for|f77|f90|jsp[ax]?|jst|j[vw]s|java|nfo|class|cfg|conf(ig)?|def|list|inc?|bash(_history|_logout|_profile|rc)?|vim(info|rc)?|sh|shar|csh|ksh|tcl|mspx?|ht|htaccess|(ht)?passwd|msg|mso|em(ai)?l|mime|log([.-]\d+(\.gz)?)?|err(or)?|access|fix|todo|alert|src|sources?|old|last|latest|bak|backup|cache|git|gitignore|rst|modules?|sample|(web)?archive(xml)?|(web)?history|pkg|du?mp|debug|te?mp|save?(deck)?|ini|inf|diz|ion|types|sql|dbf|db\d?|dat|do|so|hqx|cpt|bin|exe|dll|pif|cpl|bat|sys|cmd|obj|pot?|apk|li?nk|vcard|vcf)$  -  [NC,F]

        # Step 2
        RewriteCond  %{REQUEST_URI}       ^(.*?)([/\\]+index\.php)+$  [NC]
        RewriteRule  ^                    %1/                         [R=301,QSA,L]

        # Step 3
        RewriteCond  %{REQUEST_URI}       ^(.+?)[/\\]+$
        RewriteRule  ^[^/\\]              %1                          [R=301,L]

        # Step 4
        RewriteCond  %{REQUEST_FILENAME}  -f
        RewriteCond  %{REQUEST_URI}       !\.php$                     [NC]
        RewriteRule  ^media[/\\]+.+?$     -                           [NC,END]

        # Step 5
        RewriteCond  %{REQUEST_FILENAME}  -f
        RewriteCond  %{REQUEST_URI}       !\.php$                                   [NC]
        RewriteRule  [/\\]+themes[/\\]+\w+[/\\]+(css|fonts|images|js)[/\\]+.+?$  -  [NC,END]

        # Step 6
        RewriteCond  %{REQUEST_FILENAME}       -f
        RewriteRule  ^[^/\\]+\.(ico|txt|xml)$  -                      [NC,END]

        # Step 7
        RewriteRule  ^index\.php$         -                           [NC,END]
        RewriteRule  ^                    index.php                   [END]
    </IfModule>