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 ecomspark.com to www.ecomspark.com

RewriteEngine On
        RewriteCond %{HTTP_HOST} !^www.ecomspark.com$ [NC]
        RewriteRule ^(.*)$ http://www.ecomspark.com/$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 / http://ecomspark.com/

 

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

Redirect 302 / http://www.ecomspark.com/

 

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 http://ecomspark.com/

 

To redirect based on file extensions via the following syntax:

RedirectMatch 301 (.*)\.html$ http://www.domain.com$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 –

http://www.rapidtables.com/web/tools/redirect-generator.htm

http://www.htaccessredirect.net/

 http://www.webconfs.com/htaccess-redirect-generator.php

 

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.

2 Comments

Leave a Reply