Mod Rewrite Maintenance page that uses multiple URLs

49 views Asked by At

I have a site the uses multiple URLs for a single application. Depending on the URL of the site you will get different content. I need to create a rewrite rule that redirects a user to a different page depending on what URL the user hits.

For example: If a user visits www.foo.bar the user will be redirected to www.foo.bar/maintWWW.html But if the user visits www.bar.foo the user will be redirected to www.bar.foo/maintWWW2.html

Remember that since we are using the same application but different URLs that these 2 html pages needs to be named differently in order to serve different content.

I managed to use this but it only redirects both URLs to a single page,

 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
 RewriteCond %{REQUEST_URI} !/maintWWW.html$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintWWW.html [R=302,L]

I tried replacing the %{REQUEST_URI} with the actual URL of the site I want redirected, but it did not work.

 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
 RewriteCond http://www.foo.bar !/maintWWW.html$ [NC]
 RewriteCond http://www.foo.bar !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintWWW.html [R=302,L]

 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
 RewriteCond http://www.bar.foo !/maintWWW2.html$ [NC]
 RewriteCond http://www.bar.foo !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintWWW2.html [R=302,L]

How exactly would I get this to work? Can I include multiple URLs to be redirected to the same page? It would be nice to account for development and staging URLs as well. Example below again:

 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
 RewriteCond http://www.foo.bar http://staging.foo.bar http://dev.foo.bar !/maintWWW.html$ [NC]
 RewriteCond http://www.foo.bar !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintWWW.html [R=302,L]
1

There are 1 answers

0
Panama Jack On BEST ANSWER

You need to check HTTP_HOST in your condition to check for the domain and then redirect based on that.

 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^(www\.)?foo\.bar [NC]
 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
 RewriteCond %{REQUEST_URI} !/maintWWW.html$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintWWW.html [R=302,L]

 RewriteCond %{HTTP_HOST} ^(www\.)?bar\.foo [NC]
 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
 RewriteCond %{REQUEST_URI} !/maintWWW2.html$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintWWW2.html [R=302,L]

If you have multiple hostnames or aliases that you can use in your setup you can do this.

 RewriteCond %{HTTP_HOST} ^(www|dev|staging)\.foo\.bar [NC]
 RewriteCond %{REMOTE_ADDR} !^123\.456\.789\.000
 RewriteCond %{REQUEST_URI} !/maintWWW.html$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
 RewriteRule .* /maintWWW.html [R=302,L]