.htaccess Redirect Article

17th May 2005 at 6am Tweet a link to this on Twitter ↩

The Redirect directive maps an old URL into a new one. The new URL is returned to the client which attempts to fetch it again with the new address.

I only just discovered today how easy, useful and effective .htaccess Redirects are. Here is the code in its purist form:
Redirect file.html http://your.url/file2.html

I have a few pages that I only wanted to be available through a secure connection, https. However, I needed a fail-safe just incase someone visited the URL on the non-secure server, http. This is where .htaccess comes in.

Redirecting folders

I added the following two lines to my .htaccess file in my root directory

  1. Redirect /safe https://www.massiveblue.com
  2. Redirect /shop https://www.massiveblue.com/shop
  3. Download this code: /code/htaccess_redirect.txt

Pretty straight forward, this simply redirects in the above example http://www.massiveblue.com/safe/ to https://www.massiveblue.com. Easy!

Redirecting files

This can also easily be done for individual files. Instead of having to use the dated meta http-equiv="refresh" content="1;URL=map.html" method in the head tag we can use a .htaccess redirect that removes the need for the file in the first place, the code would look like the following:

  1. Redirect /map.html http://site.com/map/ [or]
  2. Redirect /download.html http://site.com/files/download.html
  3. Download this code: /code/htaccess_redirect2.txt

Removing [or] from the above example of course.

Creating a .htacccess file

Creating a htaccess file couldn’t be any easier. Simply open up TextEdit or Notepad for example type in your Redirect, or copy one from above and save the file as a htaccess.txt (plain text) file, no RTF. Upload the file to the root directory on your server and then rename the file to .htaccess that’s it!

Comments

I found a great extension to your redirect link idea… masking affiliate links ! It’s also a great way to hide my spread firefox link too! ;)
See, while that is a good idea, I like being able to track outgoing links.. however, thats a PHP script more than anything else.

Tricky Tek, tricky!
I hate sites that do that Tek :P
Regarding redirects, can I replace a directory name in a URL making the URL shorter?

If the actual url is a bit long, like this:
http://somedomain.com/best-sites-on-web/site1.html

Can I replace /best-sites-on-web/ with /best/ using Redirect?

Like this…
Redirect /best http://somedomain.com/best-sites-on-web

So the visitor would click on
http://somedomain.com/best/site1.html

and go to the site1.html page in the actual directory.
@ Bompa, yes. If your URL was:
http://somedomain.com/best-sites-on-web/site1.html

You could have a .htaccess redirect rule like such:
Redirect /best http://somedomain.com/best-sites-on-web/site1.html

Which would send anyone who came to the address:
http://somedomain.com/best to the correct place!
This dont work on folders

redirect /oldplace http://www.domain.com/newplace/

And since there are 800 pages, adding a redirect for each one is not on

if someone goes to /oldplace/index.html its fine but not /oldplace/notthere.html
Actually Jim, it does work. If your redirect was:
Redirect /oldplace http://www.domain.com/newplace

Any file called at /oldplace/file.html would be redirected to http://www.domain.com/newplace/file.html

Try it for yourself. I think you will find it works. :)
I can’t get this last one to work. I have a .htaccess file with this line:

Redirect /wordpress http://zuihitsu.org/etc

In the directory which resolves to this: http://home.exetel.com.au/brad/

Any tips you could give?
Hi Brad, because you are in the folder /brad/ your .htaccess file should read:

Redirect /brad/wordpress http://zuihitsu.org/etc
Hi, is it possible to redirect a folder to some sub-folder:
Eg.
Redirect /folder
http://site.com/folder/sub
It seems to recurse. Is there any way to avoid this?
Hi Ian, unfortunatly not. This will just cause a nasty loop that the browser eventually chokes on.

The best alternative would be using the meta refresh method on an index.html file:
meta http-equiv=”refresh” content=”0;URL=/folder/sub/”
Problem solved:

Redirect /folder/index.html http://site.com/folder/subfolder

Works!
By the way, thats without the index.html file even exising for anyone interested
Ah excellent, nice trick. Thanks for sharing.
No problem, I’m impressed at the speedy response, thank you.

Tony Furnell

21 October 2005, 10:48

Is it possible to do a redirect from the root folder of a site, without redirecting access to any of its subfolders?

I’d like to redirect people from an old version/domain of my site to the new domain, but still want to have access to the rest of the files on the server myself via http (e.g. using as a picture server for external sites, etc.)... can I have only the root redirected?

Tony Furnell

21 October 2005, 11:50

Actually, got it sussed:

# Setting default index page of domain
DirectoryIndex index.html
# Redirecting old page to new domain
Redirect permanent /index.html http://www.newurl.com/
Redirect permanent /otherpage.html http://www.newurl.com/otherpage.html
Redirect permanent /otherpage2.html http://www.newurl.com/otherpage2.html

Just need to ensure that the default page is specified and is redirected below.

A dummy index.html file must be present in the root folder for this to work, otherwise the user may encounter a 404 message.
Thanks for posting the solution Tony, I needed to know this too!
Are these redirects search-engine and spider friendly?

I want toredirect my only site, if any one type only anchitispat.com then autometicaly it redirect to the http://www.anchitispat.com can you give me code for this, Give me only code that i only copay and paste in a file and upload this on my root directory

Ambrish, please see the latest article I have written which discusses this exact topic.

Commenting has closed for this article. Instead use the Contact Form.