Ratings, reviews, plans and features to help you find the right web hosting provider for your site.

Redirect URLs using .htaccess

Web Hosting Articles » A simple guide to .htaccess » Redirect URLs using .htaccess



Sometimes you need to redirect some URL and/or page on your site to another one.
The feature is very useful if you have recently redesigned your site but you wish to keep the old addresses working for various reasons (you have links to these pages from other sites, some users may have the old pages bookmarked, etc).

The Apache web server provides several way for setting up redirects.

The most simple one is using the “Redirect” directive:

Redirect /folder http://www.example.com/newfolder

With such a line in your .htaccess if a visitor tries to load http://www.example.com/folder, he will be redirected to http://www.example.com/newfolder.

Recently it has been talked a lot about Permanent redirects. The good news is that you can add a status code to the Redirect directive. For example for Permanent 301 redirect you can use:

Redirect permanent /folder http://www.example.com/newfolder


Another useful directive is the RedirectMatch. With it you can use regular expressions in the redirect condition. For example

RedirectMatch "\.html$" http://www.example.com/index.php

This will redirect all requests to files that end with .html to the index.php file.

There is another more powerful way to create redirects or even create transperant redirects which requires ModRewrite. We will talk about this in the next article.


  1. How to block users from accessing your site based on their IP address
  2. How to prevent or allow directory listing?
  3. How to change the error documents – 404 Page Not Found, etc
  4. Using .htaccess for password protecting your folders
  5. Using .htaccess to block referrer spam
  6. Disable Hot-Linking of images and other files
  7. Redirect URLs using .htaccess
  8. Introduction to mod_rewrite and some basic examples
  9. Force SSL/https using .htaccess and mod_rewrite
  10. 301 Permanent redirects for parked domain names
  11. Enable CGI, SSI with .htaccess
  12. How to add Mime-Types using .htaccess
  13. Change default directory page
  14. Block Bad robots, spiders, crawlers and harvesters
  15. Make PHP to work in your HTML files with .htacess
  16. Change PHP variables using .htaccess
  17. HTTP Authentication with PHP running as CGI/SuExec
  18. Force www vs non-www to avoid duplicate content on Google
  19. Duplicate content fix index.html vs / (slash only)

Comments 10 >>

Sanjib Said,
Jul 17, 2006 @ 04:07

Thanks it works great! thank u
Venkadesan Tharshan Said,
Jul 25, 2008 @ 15:31

thanks.
soam Said,
Sep 01, 2008 @ 14:38

How to redirect my site from www.domain.com to www.domain.com/index.html
delilek Said,
Nov 01, 2008 @ 00:30

Thanks it works great! thank you
wie bali Said,
Apr 28, 2009 @ 22:12

Thank you for the tip :) You’ve inspired me to bring my .htaccess file up to speed.
redirect htaccess Said,
May 17, 2009 @ 01:19

Very useful redirect information! I think this complements and supports my .htaccess redirect article.
RSQ Said,
Jul 20, 2009 @ 10:51

thanks for this article


How do I redirect all links for www.domain.com to domain.com ?
Description of the problem:
Your website can be accessed with www.domain.com and domain.com. Since Google penalizes this due to duplicated content reasons

Solution

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
linuxcares12 Said,
Sep 19, 2009 @ 12:31

Great!!

The following link will be helpful to have a good idea on redirection using .htaccess.

http://basilvarghese.co.cc/using-htaccess-for-redirection.html
Scott Said,
Dec 07, 2009 @ 11:16

Thanks to RSQ for the great tip.

I'm using it to remove all wwws from my url - so now all my pages are only at http://scottjarvis.com

Thanks!
Sujith Said,
Jan 20, 2010 @ 08:00

hi guys

Well and I need rewrite rule for following requirement


my web pages have several links with different query strings ,my case is if click the any link in that page it must redirect in to respective page with defined variable/query string

i hope following example will help you to understand

Link: http://myweb.com/index.php?module=mine&page=login

After click the link it will like

http://myweb.com/index.php?module=mine&page=login&VCODE=777

is this possible it .htaccess please let me know the same, thanks in advance expecting your positive reply

Best regards,
Sujith
Your comments on this article

(required)

(required but never displayed)



security code



Previous: Disable Hot-Linking of images and other files Next: Introduction to mod_rewrite and some basic examples