Forum Moderators: phranque
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>...]
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>...]
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]
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]
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.
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"