Pages

Friday, 22 February 2013

How to Redirect URLs using Apache .htaccess File in PHP?

How to Redirect URLs using Apache .htaccess File in PHP?

In this apache .htaccess tutorial, we will learn how to redirect urls in PHP using .htaccess file?

.htacces is a configuration file in apache. The .htaccess file is a small text document that generally sits in the same location as your index.php or index.htm pages. It gives you the ability to interact with Apache on an individual domain-to-domain and directory-to-directory basis.

You can place the htaccess file anywhere where you'd like to control the flow of visitors. So for instance you can protect directories and redirect the traffic visiting those pages. This page will show you how to use the .htaccess file to redirect your visitors in different ways.

You can use .htaccess to redirect users to a different URL. The most basic .htaccess looks for any request for a specific page and if it finds that request, it forwards it to a new page you have specified. The syntax is:

redirect accessed-file URL-to-go-to

There are 3 parts;

(1) the Redirect command,
(2) the location of the file/directory you want redirected, and
(3) the full URL of the location you want that request sent to.

These parts are separated by a single space and should be on one line.
 
Contents of .htaccess file

1. 301 (Permanent) Redirect:

301 redirect is the most efficient and Search Engine Friendly method for webpage redirection. It's not that hard to implement and it should preserve your search engine rankings for that particular page. If you have to change file names or move pages around, it's the safest option. The code "301" is interpreted as "moved permanently".

Point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "mt-example.com" domain:

# This allows you to redirect your entire website to any other domain
Redirect 301 / http://mywebsite.com/

2. 302 (Temporary) Redirect: Point an entire site to a different temporary URL. This is useful for SEO purposes when you have a temporary landing page and plan to switch back to your main landing page at a later date:

# This allows you to redirect your entire website to any other domain
Redirect 302 /
http://mywebsite.com/

3. Redirect index.html to a specific subfolder:

# This allows you to redirect index.html to a specific subfolder
Redirect /index.html
http://mywebsite.com/newdirectory/

4. Redirect an old file to a new file path:

# Redirect old file path to new file path
Redirect /olddirectory/oldfile.html
http://mywebsite.com/newdirectory/newfile.html

5. Redirect to a specific index page:

# Provide Specific Index Page (Set the default handler)
DirectoryIndex index.html

Precautions to use before using .htaccess file:

1. Even the slightest syntax error (like a missing space) can result in your content not displaying correctly or at all.

2. Since .htaccess is a hidden system file, please make sure your FTP client is configured to show hidden files. This is usually an option in the program's preferences/options.

3. Create an empty text file using a text editor such as notepad, and save it as htaccess.txt. The reason you should save the file as htaccess.txt is because many operating systems and FTP applications are unable to read or view .htaccess files by default. Once uploaded to the server you can rename the file to .htaccess.

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.