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

Post a link to this on Twitter ↩

Comments

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:
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.

Sam 29 July 2005, 10:21 #5

@ 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!

Jim 16 August 2005, 06:40 #6

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

Sam 16 August 2005, 08:11 #7

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. :)

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 http://zuihitsu.org/etc

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

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 http://zuihitsu.org/etc

Ian 12 September 2005, 20:09 #10

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?

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 http://site.com/folder/subfolder

Works!

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

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

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.

DribbbleEndorseFacebookFoursquareInstagramTwitter