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 40 >>

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
ray Said,
Jun 07, 2010 @ 22:34

hi guys,

how do i redirects old_domain/folder/default.asp to new_domain/index.php

thx
Website design Said,
Aug 12, 2010 @ 09:37

I need to redirect

http://www.xyz.com/?a=112 tohttp://www.xyz.com/

Please advise

Thanks in advance
abhishek Said,
Oct 06, 2010 @ 10:27

hi, i have a website www.scrapansh.blogspot.com and i used domain forwarding service by www.co.cc, but the problem is, when i open the site with new domain www.scrapansh.co.cc, its shows the same address on each page or url i open, i want it to show www.scrapansh.co.cc/home/about me
like this.
can you help me to do so?
Kavita Said,
Oct 21, 2010 @ 01:33

I want code to redirect my sitemap from http://sfihomebizz.com/sitemap.xml to http://www.sfihomebizz.com/sitemap.xml
Htaccess Redirect Said,
Jan 17, 2011 @ 14:34

I've found that setting up the redirect the moment my site launches from either the http:// version to the www version or vice versa saves me loads of indexing trouble, especially with Google, who for some reason is still like the retarded stepchild of search engines. You have to tell the search engine in clear terms exactly which version of your site you want it to index or it tends to split page listings between http: and www versions and your SEO and backlinking efforts can get all screwed up.
jay Said,
Mar 04, 2011 @ 04:28

how to redirect http://www.ppp.com/contact.php to --> http://www.ppp.com/contact
Balaji Said,
Apr 14, 2011 @ 03:15

how to redirect


http://www.mywebsite.com/company/42/3/services-overview/24/services-overview/company/47.html


to ------>


http://www.mywebsite.com/
Tito Said,
May 10, 2011 @ 11:17

How i can redirect an old web to a new web with their parameters?
The command line im using for the .htaccess is redirect 301:http://200.37.59.94/juntosformajosdesarrollo.asp?por=1&en=18 to http://www.acerosarequipa.com/index.php?id=juntosforjamosdesarrollo0
Im having problems what should i do?
vince Said,
May 10, 2011 @ 12:42

@Tito

The Redirect directive cannot help as it cannot handle the GET params you have.

You will need to use ModRewrite to handle the redirect. For example:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/juntosformajosdesarrollo\.asp$
RewriteCond %{QUERY_STRING} ^por=1&en=18$
RewriteRule ^(.*)$ http://www.acerosarequipa.com/index.php?id=juntosforjamosdesarrollo0 [R=301,L]

I am not sure how your old site params relate to your new site, but basically rules as above should do the trick.
Shelly Said,
May 20, 2011 @ 19:57

Hello, thanks for your excellent tutorial. Perhaps you can help with my
problem. Any help is appreciated! I want to redirect my /index.html
to my "/" (www.sample.com). There is only one domain on the server.

I am using the following script:

#to direct index.html to www.mysite.com
RewriteEngine On
RewriteCond%{THE_REQUEST} /index\.html\ HTTP/
RewriteRule ^index\.html$ /[R=301,L]

It works, but if you type other words in your browser,
it brings up a webpage but without all the shapes/pics/etc. For
example: www.mysite.com/paper/index.html would bring up the webpage
www.mysite.com/paper with the text, but without the shapes, pics,
etc.

I want the .htaccess to redirect those types of requests (with extra words, like adding to index) to the www.sample.com/ page (and of course, without the .index.)
I tried:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\/(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$http://www.example.com/%1 [R=301,L]
It works about the same as the first example.

Bottom line, I want any requests with any extra "words" (e.g., www.mysite.com/words/index.html, etc.)just to be directed to my "index" page.
shrikant Said,
Jul 02, 2011 @ 08:48

Hello,
How i can redirect http://www.example.com/default.asp?resolutionfile=templatespath|default.asp to http://www.example.com/
please give me suggestion
thanks
Web77org Said,
Jul 07, 2011 @ 10:02

Fantastic!
The RedirectMatch is EXACTLY what I needed!
Great site. Very helpful.
Jim Said,
Jul 15, 2011 @ 10:43

I want to create a contact form and redirect it to a secure version.

http://www.jamesblakesleedesign.com/contact.aspx

point to:

https://www.jamesblakesleedesign.com/contact.aspx

Can you please tell me how to do this?
Thank you,
Jim
Anne Loula Said,
Jul 27, 2011 @ 14:20

I tried to open my subdomain.. suddenly it redirected to unknown website eg. secure.wheelerairservice.com
So I think maybe it's htaccess problem.. how to prevent it? please help;
Humberto Degenaro Said,
Aug 05, 2011 @ 08:23

Hello,

I'm from Brazil. How I can redirect for HTTPS/SSL these rules ?

RewriteRule .* index.php [L]

RewriteCond %{HTTP_REFERER} !^http://example.com.br/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.example.com.br/.*$ [NC]

Thanks.
may loc nuoc nano Said,
Aug 16, 2011 @ 11:51

thanks for share
how to redirect http://mysite.com/index.php to http://mysite.com
Steve Patterson Said,
Aug 19, 2011 @ 17:07

Thanks this worked for me. I just wished I could make it to where *.* could redirect to main, say if anyone makes any 404 it takes it to the main site or a custom error message.
Alex Said,
Sep 02, 2011 @ 11:41

Hi I have a url link to a profile with several parameters like
http://www.abusinessusa.com/view_profile.php?id=9999&city=miami&county=3816&state=FL.... and my boss asking me that he need that when he typed http://www.abusinessusa.com/business_name is redirected to the business_name profile.

How I can do that with htaccess?
thank you in advance.
Web Dizajn Said,
Sep 29, 2011 @ 10:45

Thank you for the very informative article, best regards to the author.
web design company Said,
Oct 09, 2011 @ 02:08

can any one help me to redirect below url to root domain

http://www.domainname.com/?chat

or

http://www.domainname.com/?referer=www.yahoo.com

both the above to redirect to

http://www.domainname.com

any help quickly will be appreciated
Mick Castleton Said,
Oct 14, 2011 @ 07:11

can any one help me to redirect below url to root domain

http://www.oldurl.com/ to redirect to

http://www.domainname.com keeping displaying the oldurl.com in the address bar but arriving as if it is an addon domain


advice will be appreciated
Wilfried Said,
Oct 21, 2011 @ 10:35

Redirect non-www to www (both: http + https)

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

Hi Mick
Please try this:
You can use .htaccess to redirect users to a different URL. The .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.

For example, if you want to redirect users from oldfile.html in the www directory of your account, myaccount, to newpage.html, the syntax should be

redirect /~myaccount/oldfile.html http://www.indiana.edu/~myaccount/newpage.html

Anyone going to http://www.indiana.edu/~myaccount/oldfile.html will end up on http://www.indiana.edu/~myaccount/newpage.html.

You must use a full URL even if you're going to send the users to another page on your own site.
You can also redirect an entire directory:

redirect /~myaccount/old_dir/ http://www.indiana.edu/~myaccount/new_dir/

Anyone going to http://www.indiana.edu/~myaccount/old_dir/filename.html will end up on http://www.indiana.edu/~myaccount/new_dir/filename.html.

After you set up the redirect, you should test it by going to the old URL.
Sheeko Said,
Oct 23, 2011 @ 05:21

I'll appreciate your help in finding a rewrite to force https:// on certain pages on my website i.e. Register, login, contact us etc.I don't want to force the whole site to https:// because of the mixed content warning and the slow down. Thanks!
Vince Said,
Oct 23, 2011 @ 08:13

@Sheeko

Hi, you can use for example:

RewriteEngine on

RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/register
RewriteRule ^(.*)$ https://www.your-domain.com/$1 [R=301,L]
Monalisa Said,
Oct 29, 2011 @ 09:19

Hi,
please help for redirect :
http://www.mysite.com/index.php?route=information/contact

to:
http://www.mysite.com/contact
Tracy Said,
Nov 21, 2011 @ 17:39

Could this also work in reverse? I have a site that forces https and I have a forum within a folder that I want to be http.
Murad Said,
Nov 30, 2011 @ 12:11

Hi, folks. I wanna redirect my index.php to m.php?q=cynl.nm&hl=d0 like as this. Could you help me please ?
Mike Said,
Jan 04, 2012 @ 06:58

I took a half an hour to figure it out that you can do a redirect from www. to non www. from wordpress admin panel. I tried all possible ways and all did redirects that never ended till browser stopped loading website.

The longer you life the more you learn! :)
jompom Said,
Jan 04, 2012 @ 15:25

i want to Redirect site.com/sitemap.xml to site.com


what is the method ?
Reza Said,
Jan 04, 2012 @ 20:15

Hello,

I created a new subdomain (cp.domain.com) that pointing to a remote server. now I want to redirect domain.com/cp and all of its contents to the subdomain. (Both have same contents but on different servers)
e.g. if someone try to open domain.com/cp/signup.php it redirect to same page on subdomain (cp.domain.com/signup.php) automatically.
Is this possible?
May someone help me to figure this out ?

Happy New Year
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