Dino Geek, try to help you

How to create a temporary redirect (?


Certainly, here’s how to create a temporary redirect, also known as a 302 redirect, in various contexts, using recognized sources and examples:

  1. Creating a Temporary Redirect (302) in Different Contexts

Using .htaccess in Apache:
A common way to create a 302 redirect with Apache servers is by modifying the `.htaccess` file. This file must be placed in the root directory of the web server.

```
RewriteEngine On
RewriteRule ^old-page$ /new-page [R=302,L]
```

In this example:
- `RewriteEngine On` enables the rewriting engine.
- `RewriteRule` specifies the rule for the redirection.
- `^old-page$` matches the URL path `old-page`.
- `/new-page` is the target where the user will be redirected.
- `[R=302,L]` indicates that this is a 302 (temporary) redirect and `L` means it’s the last rule to apply.

Source: Apache HTTP Server Documentation [1]

Using PHP:
In PHP, you can use the `header` function to issue a 302 redirect.

```
header(“Location: http://example.com/new-page”, true, 302);
exit();
?>
```

In this example:
- `header(“Location: …”, true, 302);` sends a raw HTTP header to trigger the redirect.
- `exit();` stops the execution of the script after the header is set to make sure no further code is executed.

Source: PHP Manual [2]

Using Nginx:
Nginx provides a simple way to create redirects in the server block of its configuration files.

```
server { listen 80; server_name example.com; location /old-page { return 302 /new-page; }
}
```

In this example:
- `listen 80;` listens on port 80.
- `server_name example.com;` specifies the server name.
- `location /old-page { return 302 /new-page; }` redirects `/old-page` to `/new-page` with a 302 status code.

Source: NGINX Documentation [3]

Using JavaScript:
For client-side redirects, JavaScript can be used to issue a 302 redirect.

```
window.location.replace(“http://example.com/new-page”);
```

This scripts:
- Uses `window.location.replace` to change the current page to `http://example.com/new-page`.

Source: MDN Web Docs [4]

  1. Examples and Considerations
    1. Example 1: Using Apache `.htaccess`
      If you have a webpage `http://example.com/products`, and you want to temporarily redirect it to a promotional page `http://example.com/special-deal`, your `.htaccess` file would look like this:

```
RewriteEngine On
RewriteRule ^products$ /special-deal [R=302,L]
```

  1. Example 2: Using PHP
    For dynamic websites, where the redirect might depend on certain conditions, PHP is ideal. Here’s an example that redirects users visiting `old-page.php` to `new-page.php`.

```
header(“Location: http://example.com/new-page.php”, true, 302);
exit();
?>
```

  1. Why Use a 302 Redirect?
    A 302 redirect is used when the redirection is temporary. Search engines primarily use this to understand that they should keep the original URL in their index and assume it will return in the future. This is unlike a 301 (permanent) redirect, where search engines transfer page rank to the new URL.

  1. Sources
    1. Apache HTTP Server Documentation. “URL Rewriting Guide.” Available at: [Apache.org](https://httpd.apache.org/docs/current/mod/mod_rewrite.html)
    2. PHP Manual. “header.” Available at: [PHP.net](https://www.php.net/manual/en/function.header.php)
    3. NGINX Documentation. “return directive.” Available at: [NGINX.org](https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#return)
    4. MDN Web Docs. “window.location.” Available at: [developer.mozilla.org](https://developer.mozilla.org/en-US/docs/Web/API/Window/location)

By exploring these examples and consulting these sources, you should be able to implement a temporary redirect using the method most suitable for your web server or environment.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use