.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
Redirect /safe https://www.massiveblue.comRedirect /shop https://www.massiveblue.com/shop- 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:
Redirect /map.html http://site.com/map/ [or]Redirect /download.html http://site.com/files/download.html- 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
Tricky Tek, tricky!
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.
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!
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
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. :)
Redirect /wordpress http://zuihitsu.org/etc
In the directory which resolves to this: http://home.exetel.com.au/brad/
Any tips you could give?
Redirect /brad/wordpress http://zuihitsu.org/etc
Eg.
Redirect /folder
http://site.com/folder/sub
It seems to recurse. Is there any way to avoid this?
The best alternative would be using the meta refresh method on an index.html file:
meta http-equiv=”refresh” content=”0;URL=/folder/sub/”
Redirect /folder/index.html http://site.com/folder/subfolder
Works!
Tony Furnell
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
—
# 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.
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.