Utilizing htaccess for Redirecting Website Traffic

Tuesday Mar 3rd 2015 by Eric Geier

Discover how you can harness the power of Apache web server's htaccess files to perform a variety of essential directory and file redirects and access management functions.

If you're running a website or other application on an Apache web server, you should have the ability to create and modify the .htaccess files in each directory. You can utilize these files to perform various essential directory and file redirects and access management functions.

Today we'll cover and provide coding and configuration examples for many of the possible redirection uses.

Enforce usage of www in URL

If you find you need or want to force the usage of the www in your website URL, you can do so by modifying the Windows Server Tutorials htaccess file on your main directory. Some search engines, like Google, are sensitive to this and essentially treat your URL with the www and without the www as two separate websites. This could have an impact on your site ranking as well.

Here is the code to add or modify for the htaccess file:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]

Remember, insert your real domain where you see example.com.

Redirect old directory or file to new one

If you remove or change directories or files, you may want to insert a redirect to the new content or a different directory's index page. This is helpful if users have bookmarked the old content while also helping ensure the old content remains listed in search engines correctly. For redirecting a directory, insert the following code into the htaccess file within the appropriate directory:

RedirectMatch 301 /old-directory(.*) /new-directory/$1

For redirecting a single file:

Redirect 301 /oldfile.htm /newfile.htm

Remember, insert your real directory or file name for both the old and new ones.

Redirect to a new or different domain

If you're moving your website to another domain, you may want to automatically forward users that visit your old domain to your new domain. Or you could have multiple domains and want to redirect to one. Whatever the case, here's the code that performs domain redirection:

RewriteEngine On
RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/$1 [L,R=301]

Remember, insert the real old and new domains in place of the "oldexample" and "newexample" content above.

Create custom error pages

As you're probably aware, the web server can trigger certain error pages when specific errors occur. Instead of having the default pages shown to users, consider crafting your own webpages. Here's the main error types and the code to point to a custom error page:

Bad Request: ErrorDocument 400 /home/www/400.html
Auth Required: ErrorDocument 401 /home/www/401.html
Forbidden: ErrorDocument 403 /home/www/403.html
Not Found: ErrorDocument 404 /home/www/404.html
Internal Server Error: ErrorDocument 500 /home/www/500.html

Depending upon the structure of your server, you may have to modify the path: /home/www/. Additionally, keep in mind you can create your own file names for these pages as well.

Change default directory page

A web server is set by default to display a certain page (like index.htm, index.html, index.php, etc) when a user tries to visit directory instead of a specific file. If you'd like to customize what file is shown when a user visits a directory, add the following code to the htaccess file in the desired directory:

DirectoryIndex new-index.html

Prevent directory listing

By default, most web servers will display a listing of all the files contained in a directory if no default index page is within that directory. However, this usually isn't desired as users could then see all the files, some of which you may not really want public. Thus for those directories without an index page, you can add the following code to the htaccess file in that directory to prevent it:

Options All -Indexes

Eric Geier is a freelance tech writer -- keep up with his writings on Facebook. He's also the founder of NoWiresSecurity, a cloud-based Wi-Fi security service, and On Spot Techs, an on-site RF site surveying and other computer services company.

Follow ServerWatch on Twitter and on Facebook

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved