HTTP

307 - Temporary Redirect

Getting a 307 Temporary Redirect means the resource temporarily moved to a new URL—follow the Location header using the same HTTP method (POST stays POST, PUT stays PUT). This client-side redirect (3xx) preserves the request method, unlike 302 which browsers may change POST to GET. Most common when resources are temporarily relocated and method must be preserved, but also appears when maintenance mode redirects, load balancing routes traffic, or temporary location changes occur.

#Common Causes

  • Frontend: Resource temporarily moved. Maintenance mode active. Load balancing redirect. Temporary location change.
  • Backend: Temporary redirect configured. Resource temporarily relocated. Maintenance mode redirect. Load balancing logic.
  • Infrastructure: Load balancer temporary redirect. CDN temporary routing. Reverse proxy temporary redirect. Maintenance mode active.

Solutions

  1. 1Step 1: Diagnose - Check DevTools Network tab Location header—307 responses show temporary redirect URL. Review redirect chain. Verify method is preserved.
  2. 2Step 2: Diagnose - Server logs show redirect configuration. Review temporary redirect rules. Check if method preservation is needed. Verify redirect is temporary.
  3. 3Step 3: Fix - Client-side: Follow Location header with same HTTP method. Preserve POST/PUT/DELETE methods. Handle redirect in application logic. Don't cache redirect.
  4. 4Step 4: Fix - Server-side: Configure 307 redirect with correct Location header. Ensure method is preserved. Set proper cache headers. Use 308 if permanent.
  5. 5Step 5: Fix - Infrastructure: Configure temporary redirects properly. Review load balancer routing. Update CDN temporary redirects. Check maintenance mode.

</>Code Examples

Fetch API: Handle 307 Temporary Redirects
1// Client-side: Handle 307 with method preservation
2async function fetchWithMethodPreservation(url, method = 'GET', body = null) {
3  let response = await fetch(url, {
4    method,
5    headers: body ? { 'Content-Type': 'application/json' } : {},
6    body: body ? JSON.stringify(body) : null,
7    redirect: 'manual', // Handle redirect manually
8  });
9  
10  if (response.status === 307) {
11    const newUrl = response.headers.get('Location');
12    console.log('Temporary redirect to:', newUrl);
13    
14    // 307 preserves original method (POST stays POST, PUT stays PUT)
15    response = await fetch(newUrl, {
16      method, // Same method as original
17      headers: body ? { 'Content-Type': 'application/json' } : {},
18      body: body ? JSON.stringify(body) : null,
19    });
20  }
21  
22  return response.json();
23}
24
25// POST request with 307 redirect
26async function postWithRedirect(url, data) {
27  let response = await fetch(url, {
28    method: 'POST',
29    headers: { 'Content-Type': 'application/json' },
30    body: JSON.stringify(data),
31    redirect: 'manual',
32  });
33  
34  if (response.status === 307) {
35    const newUrl = response.headers.get('Location');
36    // POST method is preserved
37    response = await fetch(newUrl, {
38      method: 'POST', // Still POST, not GET
39      headers: { 'Content-Type': 'application/json' },
40      body: JSON.stringify(data),
41    });
42  }
43  
44  return response.json();
45}
46
47// PUT request with 307 redirect
48async function putWithRedirect(url, data) {
49  let response = await fetch(url, {
50    method: 'PUT',
51    headers: { 'Content-Type': 'application/json' },
52    body: JSON.stringify(data),
53    redirect: 'manual',
54  });
55  
56  if (response.status === 307) {
57    const newUrl = response.headers.get('Location');
58    // PUT method is preserved
59    return fetch(newUrl, {
60      method: 'PUT',
61      headers: { 'Content-Type': 'application/json' },
62      body: JSON.stringify(data),
63    });
64  }
65  
66  return response.json();
67}
68
69// Usage
70fetchWithMethodPreservation('/api/data', 'POST', { key: 'value' });
Express.js: Configure 307 Temporary Redirects
1// Server-side: Return 307 for temporary redirects with method preservation
2const express = require('express');
3const app = express();
4
5// Temporary redirect with method preservation
6app.all('/api/temp-endpoint', (req, res) => {
7  // 307 preserves the HTTP method
8  res.redirect(307, '/api/temporary-location');
9});
10
11// POST endpoint with temporary redirect
12app.post('/api/submit', (req, res) => {
13  // Temporarily redirect POST to new location
14  // Method will be preserved (POST stays POST)
15  res.redirect(307, '/api/temp-submit');
16});
17
18// Maintenance mode with method preservation
19app.use((req, res, next) => {
20  if (process.env.MAINTENANCE_MODE === 'true') {
21    // Preserve method during maintenance redirect
22    return res.redirect(307, '/maintenance');
23  }
24  next();
25});
26
27// Load balancing redirect
28app.all('/api/load-balanced', (req, res) => {
29  const server = selectServer(); // Load balancing logic
30  res.redirect(307, `http://${server}/api/load-balanced`);
31});
32
33// Temporary location change
34app.all('/api/old-location', (req, res) => {
35  res.status(307)
36    .set('Location', '/api/new-location')
37    .set('Cache-Control', 'no-cache') // Don't cache temporary redirect
38    .end();
39});
40
41// Preserve method and body
42app.post('/api/migrate', (req, res) => {
43  // Temporarily redirect POST with body preserved
44  res.status(307)
45    .set('Location', '/api/temp-migrate')
46    .end();
47});
Nginx: Configure 307 Temporary Redirects
1# Nginx: Configure 307 temporary redirects with method preservation
2server {
3    listen 80;
4    server_name api.example.com;
5    
6    # Temporary redirect (preserves method)
7    location /api/temp-endpoint {
8        return 307 /api/temporary-location$is_args$args;
9    }
10    
11    # POST redirect (method preserved)
12    location = /api/submit {
13        if ($request_method = POST) {
14            return 307 /api/temp-submit$is_args$args;
15        }
16        proxy_pass http://backend;
17    }
18    
19    # Maintenance mode redirect
20    location / {
21        if ($maintenance_mode = 1) {
22            return 307 /maintenance;
23        }
24        proxy_pass http://backend;
25    }
26    
27    # Load balancing redirect
28    location /api/load-balanced {
29        set $backend "server1.example.com";
30        if ($arg_server = "2") {
31            set $backend "server2.example.com";
32        }
33        return 307 http://$backend/api/load-balanced$is_args$args;
34    }
35    
36    # Temporary redirect with no cache
37    location /api/old-location {
38        return 307 /api/new-location$is_args$args;
39        add_header Cache-Control "no-cache" always;
40    }
41    
42    # Note: 307 preserves method, unlike 302 which may change POST to GET

Related Errors

Provider Information

This error code is specific to HTTP services. For more information, refer to the official HTTP documentation.

307 - Temporary Redirect | HTTP Error Reference | Error Code Reference