Skip to content
Home » Web » How to Redirect Web Page

How to Redirect Web Page

Redirect Web Pages

As time goes by, we may want to change URL of website into optimized ones for search engines in order to rank higher. Furthermore, replacing old domain name into a keyword-focused one to reconstruct our URL strategies. Therefore, We need skills to redirect web pages to new URL.

Here I list 5 ways to redirect web pages.

  1. Redirect by HTML
  2. Redirect by JavaScript
  3. Redirect by jQuery
  4. Redirect by Apache HTTPD
  5. Redirect by PHP

Redirect by HTML

Just put one line to your web page below:

<meta http-equiv="refresh" content="0; url=http://www.example.com/" />

The above code has been tested in Chrome, Firefox and Internet Explorer. Please change the URL for your destination.

In case redirection does not perform what we expected, you can put another static link for manual redirection for visitors.

<p>This page has moved to <a href="http://www.example.com/">HERE!</a></p>

Redirect by JavaScript

Just use a browser element window.location to redirect the page.

<script>window.location = "http://www.example.com"</script>

It's simple and understandable. However, this code may leave HTTP_REFERER to the target website to log. If you don't like this, you should try the following code:

<script>window.open("javascript:'<meta http-equiv=\"refresh\" content=\"0; url=http://www.example.com\">'")</script>

This code will simulate opening a new window (tab) by a visitor and the best thing is that it leaves no foot print. You can test it for sure.

Redirect by jQuery

The code of jQuery redirection is a kind of JavaScript version like the above. It's just wrapped by jQuery.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>$(location).attr('href', 'http://www.example.com')</script>

Still, it has a drawback like JavaScript if you redirect to another website, which is about foot print problem that we have talked about previously.

Once again, in case users disabled JavaScript of browsers, you can put another static link for manual redirection for visitors.

<p>This page has moved to <a href="http://www.example.com/">HERE!</a></p>

Redirect by Apache HTTPD

You might want the redirection to be serviced by Apache httpd processes if the patterns of URL matches. For various situations, we have to specifically take care of them.

Redirect to New Path

If you have changed the relative path internally for some reasons, maybe SEO, you can use Redirect to map both.

[root@test ~]# vi /etc/httpd/conf/httpd.conf
...
Redirect 301 /old /new

We made the changes permanently by using the status code 301 (or using keyword permanent). Which means visitors (or crawlers) will receive 301 permanent redirections. Of course, we can also use 302 for temporary redirections.

If no status argument is given, the redirect will be "temporary" (HTTP status 302) which is the default behavior of all variants of Redirect.

Note 1: The syntax of Redirect directive is defined as:

Redirect [status] [URL-path] URL

Note 2: RedirectMatch directive sends an external redirect based on a regular expression match of the current URL.

Note 3: RedirectPermanent directive is exactly equivalent to Redirect permanent. Respectively, RedirectTemp Directive is exactly equivalent to Redirect temp.

Redirect to New Domain

If you have switched the website from one domain name to another and keep all relative paths unchanged, you can redirect all incoming requests to new domain like this:

Redirect 301 / http://www.example.com/

Redirect to Both New Domain and Path

At times, you'd like to combine above two patterns, you can redirect web pages to new domain and new path.

Redirect 301 /old http://www.example.com/new

Redirect HTTP to HTTPS

HTTPS supports Secure Socket Layer (SSL), it will encrypt sensitive content between servers and clients to simulate a private and secure browsing environment.

Now you may want all clients start to use your new HTTPS instead of common and unencrypted HTTP to connect your web server. You may check this post for more: How to Redirect HTTP to HTTPS by Web Server

Redirect by PHP

Here we made a simple function to redirect web pages to an input URL.

function redirectPermanently($url) {
    if ($_SERVER['REQUEST_URI'] && $url) {
        header("Location: $url", TRUE, 301);
        exit();
    }
}

It's very easy to use.

Leave a Reply

Your email address will not be published. Required fields are marked *