Sam Brown

.htaccess Redirect

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
  2. Redirect /shop
  3. Download this code: /code/htaccess_redirect.txt

Pretty straight forward, this simply redirects in the above example to 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 [or]
  2. Redirect /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!

Post a link to this on Twitter ↩


Tek 23 May 2005, 22:24 #1

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! ;)

Sam 23 May 2005, 22:41 #2

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!

Sean 24 May 2005, 08:21 #3

I hate sites that do that Tek :P

Bompa 10 June 2005, 20:31 #4

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:

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

Like this…
Redirect /best

So the visitor would click on

and go to the site1.html page in the actual directory.

Sam 29 July 2005, 10:21 #5

@ Bompa, yes. If your URL was:

You could have a .htaccess redirect rule like such:
Redirect /best

Which would send anyone who came to the address: to the correct place!

Jim 16 August 2005, 06:40 #6

This dont work on folders

redirect /oldplace

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

Sam 16 August 2005, 08:11 #7

Actually Jim, it does work. If your redirect was:
Redirect /oldplace

Any file called at /oldplace/file.html would be redirected to

Try it for yourself. I think you will find it works. :)

Brad Weslake 4 September 2005, 12:02 #8

I can’t get this last one to work. I have a .htaccess file with this line:

Redirect /wordpress

In the directory which resolves to this:

Any tips you could give?

Sam 5 September 2005, 10:10 #9

Hi Brad, because you are in the folder /brad/ your .htaccess file should read:

Redirect /brad/wordpress

Ian 12 September 2005, 20:09 #10

Hi, is it possible to redirect a folder to some sub-folder:
Redirect /folder
It seems to recurse. Is there any way to avoid this?

Sam 12 September 2005, 20:16 #11

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/”

Ian 12 September 2005, 20:16 #12

Problem solved:

Redirect /folder/index.html


Ian 12 September 2005, 20:18 #13

By the way, thats without the index.html file even exising for anyone interested

Sam 12 September 2005, 20:20 #14

Ah excellent, nice trick. Thanks for sharing.

Ian 12 September 2005, 20:22 #15

No problem, I’m impressed at the speedy response, thank you.

Tony Furnell 21 October 2005, 06:48 #16

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, 07:50 #17

Actually, got it sussed:

# Setting default index page of domain
DirectoryIndex index.html
# Redirecting old page to new domain
Redirect permanent /index.html
Redirect permanent /otherpage.html
Redirect permanent /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.

Sam 24 October 2005, 04:53 #18

Thanks for posting the solution Tony, I needed to know this too!

JGTC 27 October 2005, 22:24 #19

Are these redirects search-engine and spider friendly?

Sam 2 November 2005, 05:04 #20

Sure are.

Ambrish Shrivastava 18 June 2007, 22:11 #21

I want toredirect my only site, if any one type only then autometicaly it redirect to the 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

Commenting has closed for this article. Feel free to me.

Sam Brown co-founded Iterate, and was previously VP of Design at Foursquare. Based in NYC.