Cara menggunakan php refresh current page

Redirecting URLs is the practice of resolving an existing URL to a different one, effectively telling your visitors and Google Search that a page has a new location. Redirects are particularly useful in the following circumstances:

  • You've moved your site to a new domain, and you want to make the transition as seamless as possible.
  • People access your site through several different URLs. If, for example, your home page can be reached in multiple ways (for instance,
    header('HTTP/1.1 302 Found');
    header('Location: https://www.example.com/newurl');
    exit();
    1,
    header('HTTP/1.1 302 Found');
    header('Location: https://www.example.com/newurl');
    exit();
    2, or
    header('HTTP/1.1 302 Found');
    header('Location: https://www.example.com/newurl');
    exit();
    3), it's a good idea to pick one of those URLs as your preferred () destination, and use redirects to send traffic from the other URLs to your preferred URL.
  • You're merging two websites and want to make sure that links to outdated URLs are redirected to the correct pages.
  • You removed a page and you want to send users to a new page.
If you're using a platform like Blogger or Shopify, the platform may already have built-in redirect solutions. Try searching for help articles (for example, search for "blogger redirects").

Overview of redirect types

While your users generally won't be able to tell the difference between the different types of redirects, Google Search uses redirects as a strong or weak signal that the redirect target should be canonical. Choosing a redirect depends on how long you expect the redirect will be in place and what page you want Google Search to show in search results:

  • Permanent redirects: Show the new redirect target in search results.
  • Temporary redirects: Show the source page in search results.

The following table explains the various ways you can use to set up permanent and temporary redirects, ordered by how likely Google is able to interpret correctly (for example, a server side redirect has the highest chance of being interpreted correctly by Google). Choose the redirect type that works for your situation and site:

Redirect typesPermanent

Googlebot follows the redirect, and the indexing pipeline uses the redirect as a strong signal that the redirect target should be .

Use permanent redirects when you're sure that the redirect won't be reverted.
header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
4

Set up .

header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
5
header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
6 (0 seconds)

Set up .

HTTP refresh (0 seconds)JavaScript
header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
8

Set up .

Only use JavaScript redirects if you can't do server side or
header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
6 redirects.Crypto redirect

Learn more about .

Don't rely on crypto redirects for letting search engines know that your content has moved unless you have no other choice.Temporary

Googlebot follows the redirect, and the indexing pipeline uses the redirect as a weak signal that the redirect target should be canonical.

# Permanent redirect:
Redirect permanent "/old" "https://example.com/new"

# Temporary redirect:
Redirect temp "/two-old" "https://example.com/two-new"
0

Set up .

# Permanent redirect:
Redirect permanent "/old" "https://example.com/new"

# Temporary redirect:
Redirect temp "/two-old" "https://example.com/two-new"
1
# Permanent redirect:
Redirect permanent "/old" "https://example.com/new"

# Temporary redirect:
Redirect temp "/two-old" "https://example.com/two-new"
2
header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
6 (>0 seconds)

Set up .

# Permanent redirect:
Redirect permanent "/old" "https://example.com/new"

# Temporary redirect:
Redirect temp "/two-old" "https://example.com/two-new"
5 (>0 seconds)

Server side redirects

Setting up server side redirects requires access to the server configuration files (for example, the

# Permanent redirect:
Redirect permanent "/old" "https://example.com/new"

# Temporary redirect:
Redirect temp "/two-old" "https://example.com/two-new"
6 file on Apache) or setting the redirect headers with server side scripts (for example, PHP). You can create both permanent and temporary redirects on the server side.

Permanent server side redirects

If you need to change the URL of a page as it is shown in search engine results, we recommend that you use a permanent server side redirect whenever possible. This is the best way to ensure that Google Search and people are directed to the correct page. The

# Permanent redirect:
Redirect permanent "/old" "https://example.com/new"

# Temporary redirect:
Redirect temp "/two-old" "https://example.com/two-new"
7 and
# Permanent redirect:
Redirect permanent "/old" "https://example.com/new"

# Temporary redirect:
Redirect temp "/two-old" "https://example.com/two-new"
8 status codes mean that a page has permanently moved to a new location.

Temporary server side redirects

If you just want to send users to a different page temporarily, use a temporary redirect. This will also ensure that Google keeps the old URL in its results for a longer time. For example, if a service your site offers is temporarily unavailable, you can set up a temporary redirect to send users to a page that explains what's happening, without compromising the original URL in search results.

Implement server side redirects

The implementation of server side redirects depends on your hosting and server environment, or the scripting language of your site's backend.

To set up a permanent redirect with PHP, use the

# Permanent redirect:
Redirect permanent "/old" "https://example.com/new"

# Temporary redirect:
Redirect temp "/two-old" "https://example.com/two-new"
9 function. You must set the headers before sending anything to the screen:

header('HTTP/1.1 301 Moved Permanently');
header('Location: https://www.example.com/newurl');
exit();

Similarly, here's an example of how to set up a temporary redirect with PHP:

header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();

If you have access to your web server configuration files, you may be able to write the redirect rules yourself. Follow your web server's guides:

  • Apache: Consult the Apache

    # Permanent redirect:
    Redirect permanent "/old" "https://example.com/new"
    
    # Temporary redirect:
    Redirect temp "/two-old" "https://example.com/two-new"
    6 Tutorial, the Apache URL Rewriting Guide, and the Apache
    RewriteEngine on
    # redirect the service page to a new page with a permanent redirect
    RewriteRule   "^/service$"  "/about/service"  [R=301]
    
    # redirect the service page to a new page with a temporary redirect
    RewriteRule   "^/service$"  "/about/service"  [R]
    1 documentation. For example, you can use
    RewriteEngine on
    # redirect the service page to a new page with a permanent redirect
    RewriteRule   "^/service$"  "/about/service"  [R=301]
    
    # redirect the service page to a new page with a temporary redirect
    RewriteRule   "^/service$"  "/about/service"  [R]
    1 to set up the simplest form of redirects:

    # Permanent redirect:
    Redirect permanent "/old" "https://example.com/new"
    
    # Temporary redirect:
    Redirect temp "/two-old" "https://example.com/two-new"

    For more complex redirects, use

    RewriteEngine on
    # redirect the service page to a new page with a permanent redirect
    RewriteRule   "^/service$"  "/about/service"  [R=301]
    
    # redirect the service page to a new page with a temporary redirect
    RewriteRule   "^/service$"  "/about/service"  [R]
    3. For example:

    RewriteEngine on
    # redirect the service page to a new page with a permanent redirect
    RewriteRule   "^/service$"  "/about/service"  [R=301]
    
    # redirect the service page to a new page with a temporary redirect
    RewriteRule   "^/service$"  "/about/service"  [R]
  • NGINX: Read about Creating NGINX Rewrite Rules on the NGINX blog. As with Apache, you have multiple choices to create redirects. For example:

    location = /service {
      # for a permanent redirect
      return 301 $scheme://example.com/about/service
    
      # for a temporary redirect
      return 302 $scheme://example.com/about/service
    }

    For more complex redirects, use the

    RewriteEngine on
    # redirect the service page to a new page with a permanent redirect
    RewriteRule   "^/service$"  "/about/service"  [R=301]
    
    # redirect the service page to a new page with a temporary redirect
    RewriteRule   "^/service$"  "/about/service"  [R]
    4 directive:

    location = /service {
      # for a permanent redirect
      rewrite service?name=$1 ^service/offline/([a-z]+)/?$ permanent;
    
      # for a temporary redirect
      rewrite service?name=$1 ^service/offline/([a-z]+)/?$ redirect;
    }
  • For all other web servers, check with your server manager or hoster, or search for guides on your favorite search engine (for example, search for "LiteSpeed redirects").

header('HTTP/1.1 302 Found'); header('Location: https://www.example.com/newurl'); exit();6 and its HTTP equivalent

If aren't possible to implement on your platform,

header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
6 redirects may be a viable alternative. Google differentiates between two kinds of
header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
6 redirects:

  • Instant
    header('HTTP/1.1 302 Found');
    header('Location: https://www.example.com/newurl');
    exit();
    6 redirect: Triggers as soon as the page is loaded in a browser. Google Search interprets instant
    header('HTTP/1.1 302 Found');
    header('Location: https://www.example.com/newurl');
    exit();
    6 redirects as permanent redirects.
  • Delayed
    header('HTTP/1.1 302 Found');
    header('Location: https://www.example.com/newurl');
    exit();
    6 redirect: Triggers only after an arbitrary number of seconds set by the site owner. Google Search interprets delayed
    header('HTTP/1.1 302 Found');
    header('Location: https://www.example.com/newurl');
    exit();
    6 redirects as temporary redirects.

Place the

header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
6 redirect either in the
location = /service {
  # for a permanent redirect
  return 301 $scheme://example.com/about/service

  # for a temporary redirect
  return 302 $scheme://example.com/about/service
}
3 element in the HTML or in the HTTP header with server side code. For example, here's an instant
header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
6 redirect in the
location = /service {
  # for a permanent redirect
  return 301 $scheme://example.com/about/service

  # for a temporary redirect
  return 302 $scheme://example.com/about/service
}
3 element in the HTML:

<!doctype html>
<html>
  <head>
  <meta http-equiv="refresh" content="0; url=https://example.com/newlocation">
  <title>Example title</title>
  <!--...-->

Here's an example of the HTTP header equivalent, which you can inject with server side scripts:

HTTP/1.1 200 OK
Refresh: 0; url=https://www.example.com/newlocation
...

To create a delayed redirect, which is interpreted as a temporary redirect by Google, set the

location = /service {
  # for a permanent redirect
  return 301 $scheme://example.com/about/service

  # for a temporary redirect
  return 302 $scheme://example.com/about/service
}
6 attribute to the number of seconds that the redirect should be delayed:

<!doctype html>
<html>
  <head>
  <meta http-equiv="refresh" content="5; url=https://example.com/newlocation">
  <title>Example title</title>
  <!--...-->

JavaScript header('HTTP/1.1 302 Found'); header('Location: https://www.example.com/newurl'); exit();8 redirects

Google Search interprets and executes JavaScript using the Web Rendering Service once crawling of the URL has completed.

Only use JavaScript redirects if you can't do server side or
header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
6 redirects. While Google attempts to render every URL Googlebot crawled, rendering may fail for various reasons. This means that if you set a JavaScript redirect, Google might never see it if rendering of the content failed.

To set up a JavaScript redirect, set the

header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
8 property to the redirect target URL in a script block in the HTML head. For example:

<!doctype html>
<html>
  <head>
    <script>
      window.location.href = "https://www.example.com/newlocation";
    </script>
    <title>Example title</title>
    <!--...-->

Crypto redirects

If you can't implement any of the traditional redirect methods, you should still make an effort to let your users know that the page or its content has moved. The simplest way to do this is to add a link pointing to the new page accompanied by a short explanation. For example:

header('HTTP/1.1 302 Found');
header('Location: https://www.example.com/newurl');
exit();
0

This helps users find your new site and Google may understand this as a crypto redirect, (like the Loch Ness monster, its existence may be disputed; not all search engines may recognize this pseudo-redirect as an official redirect).

Don't rely on crypto redirects for letting search engines know that your content has moved unless you have no other choice. Contact your hosting provider for help with traditional redirects before resorting to crypto redirects.

Alternate versions of a URL

When you redirect a URL, Google keeps track of both the redirect source (the old URL) and the redirect target (the new URL). One of the URLs will be the canonical; which one, depends on signals such as whether the redirect was temporary or permanent. The other URL becomes an alternate name of the canonical URL. Alternate names are different versions of a canonical URL that users might recognize and trust more. Alternate names may appear in search results when a user's query hints that they might trust the old URL more.

For example, if you moved to a new domain name, it's very likely that Google will continue to occasionally show the old URLs in the results, even though the new URLs are already indexed. This is normal and as users get used to the new domain name, the alternate names will fade away without you doing anything.