Rewriting URL’s with Apaches .htaccess

The contents of this article may be out of date. It has been archived and will no longer be updated, comments are closed and the page is provided for reference purposes only.

This article looks at some common scenarios where a htaccess file can be used to manipulate a websites URL. Here we are going to look at how we can use htaccess to redirect our visitors to another location or accomplish some other common tasks.

To begin with we need to create our htaccess file. Open an empty text file and save it as “htaccess.txt” then add the code from below that is applicable. Upload the file to your home directory (can also be used in subfolder to only apply to that folder) and rename it to “.htaccess”. Renaming the file on the server will make it easier for you to edit on your Mac or PC (OSX hides files starting with a period by default).

Note: To use the examples below you will require a server running Apache with mod_rewrite enabled. If you are unsure you should contact your hosting company to confirm these settings.

Redirect a Domain to Another

Scenario:
You have moved an existing site to a new domain, if someone follows an old bookmark you would like them to arrive on your new site.

Code:
Redirect 301 / http://www.domain.com/

Result:
http://www.oldsite.com becomes http://www.domain.com/

Redirect a Static Page

Scenario:
You have a page, maybe it is well ranked in Google but that page has been replaced with a new page with a different name, or you want that page to point to a page on another domain. 301 Redirect to the rescue.

Code:
redirect 301 /old.htm http://www.domain.com/new.htm

Result:
http://www.domain/old.htm becomes http://www.domain/new.htm

Cache-Friendly File Names

Scenario:
You regularly update your CSS and javascript files and want your visitors to use the latest version instead of the cached version in their browser. This cleaver little script allows you to update your CSS and javascript files in your visitors cache by naming them in the html, on the server they remain the same name. This rewrites all files for /resources/js/anything-anynumber.js to /resources/js/anything.js and /resources/css/anything-anynumber.css to /resources/css/anything.css

Code:
RewriteRule ^resources/(js|css)/([a-z]+)-([0-9]+)\.(js|css)$ /resources/$1/$2.$4 [L]

Result:
http://www.domain/resources/js/anything-anynumber.js becomes http://www.domain/resources/js/anything.js
http://www.domain/resources/css/anything-anynumber.css becomes http://www.domain/resources/css/anything.css

Redirecting non www Traffic

Scenario:
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file.

Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

Result:
http://domain.com becomes http://www.domain.com

You may want to do the reverse e.g. have http://www.domain.com point to http://domain.com. To do this use the following:

RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain.com/$1 [R=301,L]

Redirecting to a Subfolder

Senario;
You have redeveloped your site in a subfolder of your existing website. Your new site is therefore available at http://www.domain.com/newsite/. For some reason you don’t want to move the new files to the root of the domain you can point your domain requests to the subfolder.

Code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteCond %{REQUEST_URI} !^/newsite/
RewriteRule (.*) /newsite/$1

Result:
http://www.domain.com becomes http://www.domain.com/newsite/
http://www.doamin.com/page.htm becomes http://www.domain.com/newsite/page.htm

Change Default Page

Scenario:
You want your default page (home page) to be something other than index.html. You can specify this in your htaccess file, if home.htm is not found index.htm will be used, if there is no index.htm the server will look for index.php.

Code:
DirectoryIndex home.htm index.htm index.php

Result:
http://www.doamin.com becomes (but will not show) http://www.domain.com/home.htm

Make PHP Files Appear to have HTML Extension

Scenario:
You have a site that all the pages are simple HTML however you want to add some dynamic functionality and change your pages to PHP, you can make theses pages use the .html extension so your links and search results still work. It may be the case you want to hide the fact your pages are dynamic.

Code:
AddType application/x-httpd-php .htm .html

Result:
http://www.domain.com/page.php becomes http://www.domain.com/page.htm

Redirecting Errors

Scenario:
You have moved some pages around on your site or removed them completely. Someone who has bookmarked that old page or follows a link from a search engine will receive a standard 404 error message stating that page cant be found. It would be good if we could offer a page that lets them know our sites does in fact still exist and offer some links or a search facility to help them find what they came for. This rule is also helpful when someone types in the URL incorrectly.

Code:
ErrorDocument 404 /errors/notfound.htm

Result:
http://www.domain.com/removedpage.htm becomes http://www.domain.com/errors/notfound.htm

Creating a custom 404 error page is common practice but you can also create custom error pages for the following errors using the same principle:

ErrorDocument 400 /errors/badrequest.htm
ErrorDocument 401 /errors/authreqd.htm
ErrorDocument 403 /errors/forbid.htm
ErrorDocument 500 /errors/serverr.htm

There is a lot more you can do with htaccess including rewriting PHP strings in URL’s and using htaccess for security purposes all of which is beyond the scape of this articles.

This article was posted on 09 October 2009 in Code

comments

What you have had to say about all this...

How do you hide a file extension? For example, I’d like to turn…  www.mysite.com/contact.html   into   www.mysite.com/contact

- mdoane

That's the end of this article. I hope you found it useful. If you're enjoyed this article why don't you have a look around the archives, where you can find some more tutorials, tips and general ramblings.