Forum Moderators: phranque

Message Too Old, No Replies

redirect without query string

         

Wayder

7:42 pm on Aug 29, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



I am usng a rewrite to show a maintenance page but I found that if the visitor is already on the site the query string from the previous url will be attached on the rewrite.

RewriteCond %{REQUEST_URI} !^/maintenance\.php
RewriteCond %{REQUEST_URI} !^/images/.*$
RewriteCond %{REQUEST_URI} !^/css/.*$
RewriteRule ^(.*)$ [%{HTTP_HOST}...] [R=302,L]

If a visitor is on a URL say /select.php?profile=1 then they will be redirected to /maintenance.php?profile=1.

How do I force a redirect to /maintenance.php

Thanks


Ray...

g1smd

10:26 pm on Aug 29, 2010 (gmt 0)

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



A trailing question mark on the Rule's target URL will clear the query string.

However, redirecting to a maintenance page is not a good thing to do as far as search-engine indexing is concerned. In this case, using a 302 redirect is better than using a 301, that much is true, but better still would be to directly serve a 503 response for the original request.

jdMorgan

12:42 pm on Aug 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A 503 is the correct response, but correcting/optimizing just what's there, we get:

RewriteRule !^(maintenance\.php$|images/|css/) http://%{HTTP_HOST}/maintenance.php? [R=302,L]

On Apache 2.x, you could use

ErrorDocument 503 /maintenance.php?
#
RewriteRule !^(maintenance\.php$|images/|css/) - [R=503]
#
<Files maintenance.php>
Header Set Retry-After: 3600
</Files>

to force a "503-Service Unavailable" response and tell the client to try again in one hour.

On Apache 1.x, you'd probably have to internally rewrite to a script to generate a proper 503 response, because [R=503] isn't valid on Apache 1.x.

Jim

Wayder

11:14 pm on Aug 30, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



I found that the following works well for me

ErrorDocument 503 /503.php

RewriteRule !^(503\.php|images/|css/) [%{HTTP_HOST}...] [R=503,L]

<Files 503.php>
Header Set Retry-After: 3600
</Files>

This gives me the page I want to show and a 503 response and the URL stays the same as the requested URL.

So if the URL is /select.php?profile=1 it stays /select.php?profile=1 and I get the 503 response with the maintenance page I want displayed.

Thank you for your help.

g1smd

11:31 pm on Aug 30, 2010 (gmt 0)

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



The above code provides a 302 redirect to a new URL, and then the new HTTP request for that new URL is met with a 503 response... and that is not at all the same thing, and is a serious flaw in the code.

To correct it, remove the protocol and domain name from the rule target, and define a proper ErrorDocument for the 503 code.

Wayder

11:50 pm on Aug 30, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



The only response code I get when calling the above URL (using Live HTTP headers) is:

HTTP/1.1 503 Service Temporarily Unavailable

However do you mean to alter in this way:

RewriteRule !^(503\.php|images/|css/) /503.php [R=503,L]

and how do I "define a proper ErrorDocument for the 503 code"

Thanks for your help.

jdMorgan

12:00 am on Aug 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is the correct R=503 syntax on Apache 2.x.

See the Apache mod_rewrite documentation for details.

ErrorDocument 503 /503.php
#
RewriteRule !^(503\.php$|images/|css/) - [R=503]
#
<Files 503.php>
Header Set Retry-After: 3600
</Files>

The 'target' URL -if specified- is discarded for any but 30x-series response codes.

Jim

Wayder

12:12 am on Aug 31, 2010 (gmt 0)

10+ Year Member Top Contributors Of The Month



Ahhh...

Specify the condition and just 503 it. no file to look at.

Sometimes the lightbulb holder need knocking a few times before the light comes on.

Thank you.

jdMorgan

12:39 am on Aug 31, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And again, if you're using a script for the 503 page, you could just output the Retry-after header from the script.

I'd actually recommend it, because instead of a delta time (from now) as given in the "Header set" code above, you could actually specify the date and time that you expect to have your serve back on-line. Note that the time must be given in a standard "unix time" format... Search for "HTTP 503 response code" or similar to see examples.

Jim