Simple Maintenance Mode with CodeIgniter 3

I have a CodeIgniter 3 application that I would like to take offline for maintenance but still access on my computer. I have this code in my .htaccess file that works like a charm.

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^12\.34\.56\.78$
RewriteCond %{REQUEST_URI} !construction\.html
RewriteRule ^(.*)$ /testRES/construction.html [R=302,L]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]

I have the construction.html file in the root of my application and want to use an image (either in the html or as a background image).

Where would be the best place to put the image, and what would be the path to it from my html file? I tried putting it in the root of the application, and in my assets/images folder - neither of which worked when I tried to link to it using either absolute or relative paths.

I could just use plain text in my html file, but I wanted the page to look nice for the users.

I think the RewriteRule will force request to open " /testRES/construction.html" (which could also be a PHP file).

If you have " /construction.html" in the root directory then it cannot be accessed.

I do not think the images RewriteRule is necessary.

Edit:
I was having problems and recently written Tests for URLs which may be helpful.

The RewriteRule does work. I get the application on my IP address, but on every other one I get the construction.html displayed. That part is not a problem.

My problem is being able to reference the image either as an image in the body of the file, or as a background image. All my CSS is internal and works. I just can’t get the image to display.

The RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC] was an afterthought. I was hoping that it would help, but no success. I originally had the code without that line, and it worked, but no image.

Can you supply the URL?

Have you tried using an image URL instead of a relative path?

I’m sorry, I can’t supply the URL. I know this might be frustrating, but can we talk in generalities? Where would be the best place to have the image - in the root of the application with the construction.html file? For example, if the html file is at example.com/myApplication/construction.html and example.com/myApplication is a codeigniter 3 application, what would the path be to the image?

I haven’t tried an image URL yet, but I will try that later today when I can get back to it.

Have you tried to place the jpg in the same directory of your html file and use the complete url of the image on your html?

I have most CodeIgniter 3 applications set to use a specific images folder and would recommend saving the relevant image in that folder because the image may be used again rather than have sporadic images in numerous other locations. The folder should already have the relevant read and write permissions set correctly.

I believe the HTML IMG SRC attribute expects a URL and the browser adds the relevant script to the URL if a relative path is specified.

Try a Google image search for the site and notice where other image results are located.

The construction.html file and its corresponding image maintenance.jpg are not part of the codeigniter application, so I was hoping to keep them out of even the public assets/images folder of the codeigniter application. It will only ever be used in maintenance mode. And if I have it in that folder, how can the construction.html file access it from outside the application?

Here is the folder structure and where I would like to place those two files:
app folder

  • application
  • assets
  • cache
  • system
  • .htaccess
  • composer.json
  • index.php
    - construction.html
    - maintenance.jpg

Add these rules at the beginning of your framework rewrite rules, they should work for any framework:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !=12.34.56.78
RewriteCond %{REQUEST_URI} !=/maintenance.jpg
RewriteRule . construction.html [L]

The second rule will prevent request for this specific image from being redirected to construction.html and thus allow it to be loaded by browsers.

I realize now that my above example will work only if your framework rewrite rule for routing excludes existing files from being handled by the framework and allowing them to be served directly like so:

RewriteCond %{REQUEST_FILENAME} !-f
 # sample framework rule, can vary between frameworks:
RewriteRule ^ index.php [QSA,L]

If the first RewriteCond above is missing then you need to add an empty rule, whose only purpose is to stop rewrite processing if the image is requested so that the framework rule is not executed:

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !=12.34.56.78
RewriteCond %{REQUEST_URI} !=/maintenance.jpg
RewriteRule . construction.html [L]

RewriteCond %{REQUEST_URI} =/maintenance.jpg
# stop here without rewriting anything:
RewriteRule . - [L]

# sample framework rule
RewriteRule ^ index.php [QSA,L]

If you want to use more images on the construction page then it might be more practical to put all your construction files in a separate directory and redirect to it in the same way. In that case you would probably want to use standard regular expression rules instead of the exact = matching patterns that I used.

This is a method I have previously used on sites:

index.php

<?php 
$tmp = 'construction.html';
if ( file_exists( $tmp ) ):
   http_response_code( 302 );
   require $tmp;
   exit;
endif;
 
// existing script

Add construction.html and delete or rename when not required.

The construction.html image file saved in /assets/images/maintenance.jpg to keep everything tidy :slight_smile:

No changes required to .htaccess file.

<img src='https://the site.com/assets/image/maintenance.jpg' alt='maintence in progress's />

I have both application and system folders above the root for added security and it is tidier. Only two lines to be changed in index.php ``` $application = '../application/'; $system_folder = '../system/'; // Not sure about trailing right-hand slash, typed on tablet from memory ```

Thanks for the responses. It’s getting late here, so I’ll wait until tomorrow to test them out with my application.

1 Like

[/Off topic]
Early morning here and I have just woken up :slight_smile:
[/Off topic]

1 Like

Did you manage to solve the problem?

I haven’t forgotten, just ran out of time. I’m dealing with a tight deadline so I temporarily went with a text only solution - not as classy looking. As soon as that is out of the way I will work on this.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.