How to use .htaccess for url redirect ?

If you are using Apache web server then you can redirect from .htaccess file . Htaccess follow some predefined command or directives for redirection. This file is a small text document that generally can be found in the same location as your index.php or index.htm pages.

I will tell you some basic htaccess command to redirect pages –

Know more – what is .htaccess and how to setup a .htaccess file?

Redirect to

RewriteEngine On
        RewriteCond %{HTTP_HOST} !^$ [NC]
        RewriteRule ^(.*)$$1 [L,R=301]

Suppose you want to redirect your main domain to another website then 301 redirect can be used. 301is permanent redirect.

Redirect 301 /


If you wish to redirect your main website for some temporary basis then you can use 302 redirect.

Redirect 302 /


If you wish to redirect some specific file to new directory then below command can be used –  Redirect /index.html http://www.ecomspark/blog/


To redirect a subfolder to another domain –

Redirect 301 /subfolder


To redirect based on file extensions via the following syntax:

RedirectMatch 301 (.*)\.html$$1.php

This will redirect any file with the .html extension to use the same filename but use the .php extension instead.

If you are not a technical person and in urgency you need to redirect some webpages then some online Htaccess command generator tools also available. It can be used to write generated code in Htaccess file. Here are some tools links are –


what is .htaccess and how to setup a .htaccess file?

Comments are welcome to make it more clear for all. I am still learning so please comments any useful information.


Leave a Reply