Forum Moderators: phranque

Message Too Old, No Replies

Send all requests to a single static file

         

hornet

8:40 pm on Jul 9, 2007 (gmt 0)

10+ Year Member



What's the best/simplest way to forward/redirect/rewrite all user requests to a single html file (that contains images and javascript)? Think of it like an "outage" page -- and all requests (whether they exist or not, whether http or https) should be sent to this "outage" page.

For instance, if I have <my_site>/<content_root>/sitedown.html and the user tries to hit something like <my_site>/<content_root>/abc.jsp or <my_site>/<content_root>/abc.html or <my_site>/<content_root>/non_existent_file.html -- all of these should bring up the sitedown.html page.

Was thinking this could be done with some rewrite rules in httpd.conf, but when I test a file that doesn't exist, I get a 404 error. This is what I have right now.

RewriteEngine on
RewriteRule ^(.*) [<my_site>...]

hornet

9:10 pm on Jul 9, 2007 (gmt 0)

10+ Year Member



Some additional info...

Appears to work correctly when requesting a file over http -- either an existing or non-existing file) -- both redirect to the sitedown page. However, when requesting the same files via https, I get the actual page or 404 (for the non-existent file).

Here's what I have at the bottom of the httpd.conf:

LoadModule ibm_ssl_module modules/mod_ibm_ssl.so
<IfModule mod_ibm_ssl.c>
Listen 443
<VirtualHost *:443>
SSLEnable
</VirtualHost>
</IfModule>
KeyFile "/opt/IBM/HTTPServer/keys/key.kdb"

RewriteEngine on
RewriteRule ^(.*) [<my_site>...]

g1smd

10:35 pm on Jul 9, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>> both redirect to the sitedown page <<

That isn't a redirect, it is a rewrite.

They are not the same thing. You need to be very clear which one you actually want, because there are nasty side-effects to using the wrong one.

SteveWh

5:49 am on Jul 10, 2007 (gmt 0)

10+ Year Member



It should be something like the following:

If you enable the first line, you can do testing without disrupting your site. If only does the rewrite if you are making the request yourself. Change the digits to your IP address at the time you are doing the test.

The second RewriteCond line is required to prevent endless looping.

# RewriteCond %{REMOTE_ADDR} ^111\.222\.333\.444 [NC]
RewriteCond %{REQUEST_FILENAME} !(/sitedown\.html) [NC]
RewriteRule ^(.*)$ [yourdomain.com...] [R=302,L]

However, your sitedown page must not contain references to any images or external javascript files, css files, or anything else external. The above code will rewrite every request that isn't for sitedown.html. If you want to allow other files, you'll have to add additional RewriteCond lines that exclude them, too.

[edited by: SteveWh at 5:57 am (utc) on July 10, 2007]

hornet

2:48 pm on Jul 10, 2007 (gmt 0)

10+ Year Member



SteveWh,

I tried your suggestion, with 2 changes (making the RewriteRule go to https instead of http and adding app-context-root), but I still get the same behavior -- anything I request via http serves up the sitedown page, but a non-existent page requested via https gives 404, and an existing page requested via https gets served normally.

Since my sitedown page DOES contain images and javascript and css files, what would be the RewriteCond lines to add to allow any files in these directories be served normally:

<DocumentRoot>/<app-context-root>/images
<DocumentRoot>/<app-context-root>/javascript
<DocumentRoot>/<app-context-root>/css

Here's what I have so far:

LoadModule ibm_ssl_module modules/mod_ibm_ssl.so
<IfModule mod_ibm_ssl.c>
Listen 443
<VirtualHost *:443>
SSLEnable
</VirtualHost>
</IfModule>
KeyFile "/opt/IBM/HTTPServer/keys/key.kdb"

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!(/sitedown\.html) [NC]
RewriteRule ^(.*)$ [yourdomain.com...] [R=302,L]

SteveWh

4:28 pm on Jul 10, 2007 (gmt 0)

10+ Year Member



Assuming that your .htaccess is in public_html, you need to adjust some of the paths accordingly. For example, if sitedown.html is in /app-context-root/, then you need to change the second line to

RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/sitedown\.html) [NC]

It is also that line which in general allows filenames to not be rewritten, so you need to add a line for each folder you want to allow. To allow entire folders, you'll have to construct regular expressions that match the folder names. (For some reason, the forum message editor is stripping out spaces in some places where they're needed, such as before each "!".) So the revised version will be more like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/sitedown\.html) [NC]
RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/images/) [NC]
RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/javascript/) [NC]
RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/css/) [NC]
RewriteRule ^(.*)$ [yourdomain.com...] [R=302,L]

I've never dealt with https at all. From the behavior you describe, is it possible it is being handled in a completely different system, where it needs its own httpd.conf (or .htaccess) or its own section in those files that relates to accesses by https? The reason I suggest that is this:

a non-existent page requested via https gives 404, and an existing page requested via https gets served normally

That's exactly "normal" behavior, as though the rewrite code isn't even being looked at when a https request comes in.

You might get lucky and someone who is an expert at this might come along to help, but you might wind up having to spend a day or two studying regular expressions, mod_rewrite, and even https handling.

The Apache mod_rewrite documentation is at [httpd.apache.org...] and the pages it links to.

Unfortunately, these often are not easy, and many situations, such as yours, don't have ready-made examples on the web.

jdMorgan

4:33 pm on Jul 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just stopping in quickly here...

Carefully evaluate the scope and precedence of your <VirtualHost> and <IfModule> containers. You'll need to put your rewrite rules where they will be executed for each server (http and https) as needed.

Jim

hornet

6:32 pm on Jul 10, 2007 (gmt 0)

10+ Year Member



Thanks, jdMorgan and SteveWh! Looks like this format solves the problems:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/sitedown\.html) [NC]
RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/images/) [NC]
RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/javascript/) [NC]
RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/css/) [NC]
RewriteRule ^(.*)$ [yourdomain.com...] [R=302,L]

LoadModule ibm_ssl_module modules/mod_ibm_ssl.so
<IfModule mod_ibm_ssl.c>
Listen 443
<VirtualHost *:443>
SSLEnable
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/sitedown\.html) [NC]
RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/images/) [NC]
RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/javascript/) [NC]
RewriteCond %{REQUEST_FILENAME}!(^/app-context-root/css/) [NC]
RewriteRule ^(.*)$ [yourdomain.com...] [R=302,L]
</VirtualHost>
</IfModule>
KeyFile "/opt/IBM/HTTPServer/keys/key.kdb"

jdMorgan

8:04 pm on Jul 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would recommend a 301-Moved Permanently redirect instead of a 302-Found if you wish your HTTPS pages to be listed as HTTPS in the search results.

Jim