How to Redirect By .htaccess File ?

Home / Website Design and Management / htaccess file / How to Redirect By .htaccess File ?

Redirects enable us to direct web site visitors from one document within your web site to another directory within your website or another website’s directory or file. This is useful if you want to direct your traffic from one websites to another , or from one directory to another directory. Mostly useful for traffic direction and SEO services.

htaccess redirect

There are several kinds of redirects using .htaccess file

301 (Permanent) Redirect:  Direct your whole website 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 “powerhoster.com” domain:

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

302 (Temporary) Redirect:  Direct your whole website 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://powerhoster.com/

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

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

Redirect to a specific index page:
# Provide Specific Index Page (Set the default handler)
DirectoryIndex index.html

Redirect an old directory to a new directory, or to the new directory in another website.

#directory old directory to new directory
Redirect /old_direstory/ http://www.powerhoster.com/new_directory/index.html

The above line tells the Apache Web Server that if a visitor requests a documents located in the directory ‘old_directory’, then to display the document ‘index.html’ located in the directory ‘new_directory’.

Rewriting product.php?id=12 to product-12.html
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1

Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz


RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1

Leave a Reply

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