How to Redirect URL

There are several ways to redirect a user from one URL to another URL. There might be several reasons to do this. I am describing different ways to redirect URL or page quickly and seamlessly.

1. Redirect URL using JavaScript

You can put the following code in your page where you want user to redirect:

This method fits well when you don’t want to fetch “Header already sent” error generated by PHP if you already have made some output before calling Header function (described below) in PHP.

2. Use a meta tag to redirect to another URL

You can type the following in head section of your page:

“0” here stands for number of seconds before redirect happens. Please note that pages with meta refresh are often filtered by search engines since it’s a known spam technique.

3. Redirect using an htaccess file

If you’re going to permanently stop using the old URL and you want to make sure that search engines only “know about” the new site, this method is best suitable. Ensure if your website is running on an Apache server.

htaccess is a notepad file that named as .htaccess and stored in your directory. Paste the following code into that file:

“L” indicates that it’s the last instruction and “R” means redirect, and “301” means a permanent redirect.

4. Redirection using PHP

In PHP you can redirect using the header() function.

It is important that the script has not printed any HTML before you make the redirection even a space to, or you will get a warning: “Cannot modify header information – headers already sent…”

5. Redirect URL in WordPress

We have created a tutorial on using wp_redirect function() to redirect a user from one URL to another. Follow this link to provide redirection in your WordPress website:

http://fellowtuts.com/wordpress/redirect-another-url-wordpress/

If you want to redirect to page rather than URL, it’s quite simple. Just use “http://yourdomain.com/yourpage/” instead of “http://yourdomain.com/”.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.