How to Redirect a URL:

This post deals with how you can redirect one URL (ie, web address) to another, so that if one goes to (say) http://www.example.com/old-page.html, he/she is automatically redirected to http://www.example.com/new-page.html.

Prerequisites:

The original URL must be on a website that you control (for example, it belongs to you). You cannot redirect some random web address on the Internet to another. The destination URL, however, can be anywhere you like.
Your website must be on a machine running the Apache web server software. We will be modifying one of the server's configuration files, specifically,.htaccess.
Your web host must allow you to modify the server settings through the use of .htaccess directives. Most commercial web hosts permit this.

How to Redirect One Web Page to Point to Another:

To direct one URL to another, for example, from http://www.example.com/old-page.html to http://www.example.com/new-page.html, all that is needed is to add a line like the one below to your.htaccess file located in the directory (folder) of the original page.

Redirect permanent /old-page.html http://www.example.com/new-page.html

The words "Redirect permanent" tells search engines and browsers that the address, http://www.example.com/old-page.html has permanently changed to http://www.example.com/new-page.html.

You can also use "Redirect 301" in place of "Redirect permanent" if you prefer. They mean the same thing. "301" is the numeric status code the server sends to the browser, and it means "permanent".

Notice that you cannot specify the full URL of the old page. Just state the part after your domain name. So, if the page to be redirected is http://www.example.com/old-page.html, you should only say /old-page.html. However, the destination of the redirection can be any URL, even one that is on some other website.

If the redirection is temporary for some reason (for example, it's only for a day while you update old-page.html), use the following directive instead:

Redirect temp /old-page.html http://www.example.com/new-page.html

You can also say "Redirect 302" instead of "Redirect temp". "302" is the numeric status code for a temporary redirection and has the exact same meaning as the "temp" directive given above.

If your original file is in a sub-directory and not the main directory of your website, include the sub-directory name, as follows.

Redirect permanent /offers/winter-specials.html http://www.example.com/expired-offers.html

The above redirects all requests for

http://www.example.com/offers/winter-specials.html to http://www.example.com/expired-offers.html.

The "offers" directory must be specified in the directive even though the .htaccess file is already in the "offers" folder.

Be sure to hit the ENTER key (or the RETURN key on a Mac) after the directive, so that the file ends on a blank line. That is, when you move your text cursor to the end of the file in the editor, the cursor should be on a blank line, and not the tail end of the line you just typed.

Another way to Redirect the URL if your site is inside the folder of the root:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} wordpress 
RewriteRule ^(.*)$ https://example.com/wordpress/$1 [R=301,L] 
# BEGIN WordPress
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L] 
# END WordPress

In the above coding just change the URL of your website name and execute.


Where Do I Find The .htaccess File?

First connect to your website using an FTP program. Core-ftp and FileZilla is the main FTP software programs widely used.

Go to the directory (ie, folder) of the original file (for example, the directory containing old-page.html), and look for any .htaccess file there.

If you use Core-ftp and you don't see any .htaccess file, you may have to click "Server" on the menu bar, and select the line "Force showing hidden files" [sic] in the drop-down menu that appears to put a tick there. If the file is already visible in the directory, you don't need to enable this setting.

If there is an existing .htaccess file, download it to your computer. If you don't see any, and you are sure your FTP software is set up to display all hidden files, don't worry. The .htaccess file doesn't exist by default. It will only be there if you have created one before or if your web host has put one there. If none exists, you will make a new one in the next section.

Creating or Modifying the .htaccess File

Whether you are creating a new .htaccess file or editing an existing one, you will need to use a plain text editor, also known as an ASCII text editor.

Windows users already have one in the form of a program called Notepad. Just click the Start menu, type "notepad" and hit the ENTER key.

Under no circumstances should you use a word Processor like Microsoft Word, Word Pad, Write, Open Office, LibreOffice, or the like.

Open .htaccess in the editor, and type the appropriate directive on a new line. Remember to change the URLs in my examples so that they refer to the ones you want. If you don't have an existing .htaccess file, just type the line into a new blank document.

Save the file as ".htaccess". Make sure the file name begins with a full stop (ie, period).

If you use Notepad, make sure you type ".htaccess" including the quotation marks, or it will”.txt” extension to the file name behind your back, saving it as ".htaccess.txt".

Even worse, you won't be able to spot this annoying change unless you have also configured windows to show the file extensions , so make sure you don't forget the quotation marks.

Once you're done, upload the file back to your site, placing it in the directory of the original file (that is, the same folder where old-page.html lives, in the case of our example above).

If your FTP software needs you to specify the mode in which to upload, transfer the file in ASCII mode (not binary). Most software (including Core FTP and FileZilla) should automatically do this correctly, unless you have messed with the default settings.

Now test that your redirection works by typing your old URL into the browser. You should automatically be sent to the new one.

I also recommend that you test the URLs of some of the other pages in the directory where you placed the .htaccess file, to make sure that you didn't accidentally make an error that renders those pages inaccessible. That is just go to some of those pages in your browser to make sure that they still load properly.

Categories: LINUX