Web development often involves redirecting users from one URL to another for various purposes, such as moving to a new domain, improving navigation, or implementing security.
Under the Server-Side Redirect and Client-Side Redirect, there are few redirect types are exist.
Server-Side Redirect:
Client-Side Redirect:
Every redirect method has a purpose and effect, some of them are outdated and are not SEO-friendly, the details about redirection types are below.
Effect | SEO-Friendly | ||
Server-Side Redirect | 301 Redirect (Permanent Redirect) | Indicates a URL has permanently moved to a new location. Search engines transfer the majority of SEO value (link equity) to the new URL | Yes |
302 Redirect (Temporary Redirect) | Indicates a URL has temporarily moved. Search engines don’t transfer SEO value as the move is not permanent. | If this redirect remains long, they can treat it as a 301 and update the pages, and 302 will be SEO-friendly. | |
307 Redirect (Temporary Redirect) | The HTTP/1.1 version of a temporary redirect (similar to 302). Temporary redirects while retaining HTTP methods like POST or GET. | – | |
308 Redirect (Permanent Redirect) | Signals that a resource has permanently moved to a new URL and ensures that the HTTP request method (e.g., POST, PUT) and body remain unchanged. | Similar to a 301 redirect, it passes link equity to the new URL, ensuring the SEO impact of the original page is retained. | |
Client-Side Redirect | JavaScript Redirect | Redirects using client-side JavaScript code. | Not SEO-friendly and depends on JavaScript being enabled in the user’s browser. |
Meta Refresh Redirect | Implemented at the page level rather than the server. | Poor for SEO, often considered outdated. |
301 redirect using PHP.
header('HTTP/1.1 301 Moved Permanently');
header('Location: https://www.example.com/newurl');
exit();
301 redirect on Apache server using .htaccess
# Redirecting a single page
Redirect 301 /old-page.html https://www.example.com/new-page.html
# Redirecting the entire domain
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldexample\.com$ [NC]
RewriteRule ^(.*)$ https://www.newexample.com/$1 [R=301,L]
# Redirecting a folder
Redirect 301 /old-folder/ https://www.example.com/new-folder/
302 redirect using PHP.
header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
301 redirect on Apache server using .htaccess
# Redirecting a single page
Redirect 302 /temp-page.html https://www.example.com/another-temp-page.html
JavaScript redirection is a method of redirection from source to destination by using JavaScript script.
Not: JavaScript redirection does not affect the HTTP response code; it does not result in a 3xx response code also, this method is not SEO-friendly. Only use JavaScript redirects if you can’t do server-side or meta-refresh redirects.
if (window.location.origin === "https://example.com/page-a") {
var path = window.location.pathname;
window.location.replace("https://example.com/page-b" );
}
Also, you can use window.location.pathname
it for JavaScript redirection.
if (window.location.origin === "https://example.com/page-a") {
var path = window.location.pathname;
window.location.replace("https://example.com" + path );
}
JavaScript Redirect Example:
The redirect method below redirects the page 5 seconds after the page loads by HTML meta tag.
<meta http-equiv="refresh" content="5;url=https://www.example.com/page-a">
The meta code above must be inside <head></head> tags.
If you are sure that the redirect page won’t be reverted, the best redirection method is 301 redirect.
Source: https://developers.google.com/search/docs/crawling-indexing/301-redirects